An example of using TextView to realize the effect of the horse lamp in Android application

Source: Internet
Author: User

How to achieve two textview of the effect of the horse, if the use of conventional practice, can only make a textview function. The method is now as follows:

1. Add four attributes to TextView

Android:ellipsize= "marquee"
android:focusable= "true"
android:focusableintouchmode= "true"
Android: Singleline= "true"


Implement the TextView class, implement three constructors, and overload the IsFocused method.

public class marqueetext extends textview {  public marqueetext (Context context)  { super (context);  }  public  MarqueeText (context context, attributeset attrs)  {     super (
CONTEXT, ATTRS); &NBSP}  public marqueetext (context context, attributeset attrs, int 
Defstyle)  {     super (Context, attrs, defstyle)  }   @Override  public boolean isfocused ()  {     return true;  }   .} 

The



uses its own implemented TextView class in the Main.xml file.

This achieves the effect of two horse lights. Here is the main rewrite the Isfocuse method, so that the default two TextView are Focuse, so the two textview can be happy.



Android implements multiple TextView and displays a marquee effect

If you need to display a long copy on a smaller Android page, you cannot display the full, It is natural to think of the marquee--in the TextView effect, but the Android implementation of the marquee effect requires control to get focus, when a control Requestfocus (), the other control will be the focus of the grab, This causes only one control to get the focus at the same time. So, what if you let multiple controls get and hold the focus at the same time? Or, "cheat" the Android system so that it thinks that multiple controls hold the focus, that is, each control is in the focus state.

with the above analysis, the problem translates into how to keep multiple controls in focus at the same time, which requires rewriting some of the TextView methods to "trick" the Android system so that each control requestfocus () after it is in the focus state. And not be deprived of focus, you can reach multiple controls at the same time "hold" the focus, the code is as follows:

Public class marqueetextview extends textview {    public  Marqueetextview (context context)     {        
This (context, null);    &NBSP}          public marqueetextview (context  context, attributeset attrs)     {      
  super (Context, attrs);                  setfocusable (
true);
        setfocusableintouchmode (TRUE);                  setsingleline (
);
        setellipsize (TextUtils.TruncateAt.MARQUEE);
        setmarqueerepeatlimit (-1);     }          public marqueetextview (context context,  Attributeset attrs, int defstyle)     {      
  super (Context, attrs, defstyle);                  setfocusable (
true);
        setfocusableintouchmode (TRUE);                  setsingleline (
);
        setellipsize (TextUtils.TruncateAt.MARQUEE);
        setmarqueerepeatlimit (-1);    &NBSP}      @Override     protected void  Onfocuschanged (Boolean focused, int direction, rect previouslyfocusedrect)      {        if  (focused)         {             super.onfocuschanged (Focused, direction,
 previouslyfocusedrect);        &NBSP}     }        
   @Override     public void onwindowfocuschanged (boolean focused)     {        if  (focused)          {            
Super.onwindowfocuschanged (focused);        &NBSP}     }        
   @Override     public boolean isfocused ()     {         return&nBsp;true;    &NBSP}}




Due to the normal execution of the properties of the marquee:

Android:focusable= "true", android:focusableintouchmode= "true", android:singleline= "true", android:ellipsize= " Marquee

are set in code, you do not need to add these 4 properties when referencing this control.

Related Article

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.