Android:textview Implement text Merry effect (spoofing system to get persistent focus)

Source: Internet
Author: User

In general, we want to implement the merry effect of the text in the XML file to set this

    <textview        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android: Singleline= "true"        android:ellipsize= "marquee"        android:focusable= "true"        Android: Focusableintouchmode= "true"        android:text= "@string/lyric"/>

We all know it and don't explain it.

Does the Singleline:boolean type show text only on one line instead of multiple lines

Ellipsize: Scrolling effect, inside there (None,start,middle,end,marquee), where none means normal display of text, even if the line is not complete, there is no effect. Star, that is, if the text is not fully displayed on a line, at the beginning of the display ..., in the same way, the end of the last line of text after adding ..., middle is all the text displayed in one line, if the text too much, in the middle to join .... Maybe I can't explain it, that's probably how the reader can test it himself. As for marquee is the text merry effect.

Of course, if you only set the text, it won't scroll. Also make TextView get the focus.

Focusable: Can focus, Boolean

The Focusableintouchmode:boolean type.

Whether to get focus in touch mode.

When you set up these deployments on your phone, it is clear that merry effects will be achieved. The effect is as follows:


But if you add an edit box control to the activity instance, you'll see that the merry effect disappears when you click on the edit box.

Just like this.


What is this for?

Because clicking on the edit box will get the screen focus, as there is usually only one focus on the screen, TextView loses focus and does not scroll. What are we going to do now?

Then deceive the system. Tell it that our TextView also have the focus. Yes, two points.

How to do? We'll create a new textview of our own.

First we create a new class named Mytextview inherited TextView, rewrite the method inside, there are three methods are necessary, as we always in mainactivity inside rewrite OnCreate method, the role of what I do not know. Curious students please Baidu. haha ~

We need to know how the system determines whether a control gets the focus?

public Boolean isFocused () {//TODO auto-generated method Stubreturn super.isfocused ();}

That's the way it is. Before we say we're going to cheat the system, our textviwe is in the spotlight. So we always return true in this method is good haha is not special rogue ...

When you're done, don't forget to deploy our TextView to the layout file.

Layout code:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Fill_ Parent "    android:layout_height=" fill_parent "    android:orientation=" vertical ">    < Com.example.textview.MyTextView        android:layout_width= "wrap_content"        android:layout_height= "Wrap_ Content "        android:ellipsize=" marquee "        android:singleline=" true "        android:text=" @string/lyric "/>    <edittext        android:layout_width= "fill_parent"        android:layout_height= "Wrap_content"/></ Linearlayout>

Mainactivity.class

Package Com.example.textview;import Android.os.bundle;import Android.app.activity;import android.view.Menu;public Class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main);}}

Mytextview.class

Package Com.example.textview;import Android.content.context;import Android.util.attributeset;import Android.view.windowid.focusobserver;import Android.widget.textview;public class Mytextview extends TextView{public Mytextview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle);//TODO auto-generated con Structor stub}public Mytextview (context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated Constructor Stub}public Mytextview (context context) {super (context);//TODO auto-generated constructor stub}@ Overridepublic Boolean isFocused () {//TODO auto-generated method Stubreturn true;}}

Yes, attached. All see the cursor in the edit box ~



Android:textview implements the text merry effect (spoofing the system to get persistent focus)

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.