Android: TextView achieves the text drive effect (the spoofing system gets the persistent focus)

Source: Internet
Author: User

Android: TextView achieves the text drive effect (the spoofing system gets the persistent focus)

In general, we need to set this setting in the xml file to achieve the effect of text drive lights.

  <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"></textview>

I will not explain what everyone understands.

SingleLine: boolean indicates whether to display text only in one row rather than multiple rows.

Ellipsize: Scroll effect, which includes (none, start, middle, end, marquee). none indicates that the text is displayed normally, even if one line is not completely displayed, there is no effect. Star, that is, if the text is not completely displayed in one line, it is displayed at the beginning ..., similarly, the last text in the end line is appended ..., middle indicates that all text is displayed in one line. If there are too many texts, add... in the middle .... I may not be clear about the explanation, so that the reader can test it by himself. As for marquee, it is the result of text drive.

Of course, if you only set the text, it will not scroll. The TextView must also be used to obtain the focus.

Focusable: whether to focus, boolean Type

FocusableInTouchMode: boolean type.

Whether to obtain the focus in touch mode.

When you deploy these settings on your mobile phone, it will obviously achieve the drive-by effect. The effect is as follows:

However, if you add an edit box control to the Activity instance, click the edit box and you will find that the drive lamp disappears.

Just like this

Why?

Because you click the edit box, the edit box gets the screen focus. Generally, only one of the screen focus is displayed, and TextView does not scroll because it loses the focus. What should we do at this time?

Then cheat the system. Tell it that our TextView is also focused. Yes, there are two focal points.

How to do it? Let's create a new TextView.

First, we create a class named MyTextView that inherits TextView and override the methods in it. Three of them are required, just as we always rewrite the OnCreate method in MainActivity, I don't know what the role is. For 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();}

This method is used. As mentioned above, we want to cheat the system. Our TextViwe has a focus. So we can always return true in this method. Haha, isn't it really rogue...

After completing these steps, do not 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"></edittext></com.example.textview.mytextview></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 constructor 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;}}

Attached. See the cursor in the editing box ~

 

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.