Android Phone Defender--custom control (get focus of TextView)

Source: Internet
Author: User

This article address: http://www.cnblogs.com/wuyudong/p/5906735.html, reprint please indicate source address.

This article will implement the title bar below the text in the TextView the effect of the marquee, is to be a line of text horizontal cycle scrolling, the effect is as follows:

The implementation code is as follows:

    <!--android:ellipsize= "End" adds the location of the ellipsis point -    <!--to make the text appear a marquee effect, you must get the focus -    <!--android:marqueerepeatlimit= "Marquee_forever" scrolling property -    <!--The custom control has a scrolling effect (in fact, it re-textview the original, so that it can always get the focus) -    <TextViewAndroid:text= "It's a marquee, it's a marquee, it's a marquee, it's a marquee, it's a marquee,"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:ellipsize= "Marquee"android:focusable= "true"Android:focusableintouchmode= "true"Android:marqueerepeatlimit= "Marquee_forever"android:padding= "5DP"Android:textcolor= "#000"Android:singleline= "true"        />

If other places also need such a marquee effect, copying code is more troublesome. Here, a custom control is used to implement the scrolling effect (in fact, to re-textview the original, so that it can always get the focus)

Create a new package view that specifically puts the custom control file

New Focustextview Class

Add code:

 PackageCom.wuyudong.mobilesafe.view;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView;/** * @authorWuyudong * Can get focus of custom TextView **/ Public classFocustextviewextendsTextView {//use to create controls through Java code     PublicFocustextview (Context context) {Super(context); }    //called by the system (with attribute + Contextual environment construction method)     PublicFocustextview (Context context, AttributeSet attrs) {Super(context, attrs); }    //constructed by system call (with attribute + Contextual environment construction method + layout file defined style file)     PublicFocustextview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); }    //override method to get focus@Override Public BooleanisFocused () {//return super.isfocused ();        return true; }}

Replace the layout code with:

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <TextViewstyle= "@style/titlestyle"Android:text= "Feature List" />    <!--android:ellipsize= "End" adds the location of the ellipsis point -    <!--to make the text appear a marquee effect, you must get the focus -    <!--android:marqueerepeatlimit= "Marquee_forever" scrolling property -    <!--The custom control has a scrolling effect (in fact, it re-textview the original, so that it can always get the focus) -    <!--<textview android:text= "It's a marquee, it's a marquee, it's a marquee, it's a marquee, it's a marquee ," android:layout_width= "Match_        Parent "android:layout_height=" Wrap_content "android:ellipsize=" marquee "android:focusable=" true "        Android:focusableintouchmode= "true" android:marqueerepeatlimit= "Marquee_forever" android:padding= "5DP" Android:textcolor= "#000" android:singleline= "true"/> -    <Com.wuyudong.mobilesafe.view.FocusTextViewAndroid:text= "It's a marquee, it's a marquee, it's a marquee, it's a marquee, it's a marquee,"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:ellipsize= "Marquee"Android:marqueerepeatlimit= "Marquee_forever"android:padding= "5DP"Android:textcolor= "#000"Android:singleline= "true">    </Com.wuyudong.mobilesafe.view.FocusTextView></LinearLayout>

Summarize Custom Controls

Custom Control Authoring Process
Create a textview that will get the focus by default

1. Create a class to inherit to Textview,focustextview

2, rewrite its construction method

 Public classFocustextviewextendsTextView {//use to create controls through Java code     PublicFocustextview (Context context) {Super(context); }    //called by the system (with attribute + Contextual environment construction method)     PublicFocustextview (Context context, AttributeSet attrs) {Super(context, attrs); }    //constructed by system call (with attribute + Contextual environment construction method + layout file defined style file)     PublicFocustextview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); }    //override method to get focus@Override Public BooleanisFocused () {//return super.isfocused ();        return true; }}

3, the original TextView on the Isfocus method is modified by default to be able to get focus

    // override method to get focus     @Override    publicboolean  isFocused () {        //  return Super.isfocused ();        return true ;    }

4. Use of the process

Gets the full path name of the current class, exists as a label in the XML, and the remaining properties are used in the same manner as TextView

Android Phone Defender--custom control (get focus of TextView)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.