Textview is used in Android to realize the effect of text running lamp

Usually, we need to set this in the XML file in order to achieve the riding light effect of text

We don't explain what we all understand.

Singleline: Boolean whether to display text on one line instead of multiple lines

Ellipsize: scrolling effect, including (none, start, middle, end, marquee), where none indicates normal text display. Even if one line is not completely displayed, there is no effect. Star means that if the text is not completely displayed in one line, it is displayed at the beginning... Similarly, add... After the last text in the end line, and middle means that all the text is displayed in one line. If there are too many text, add... In the middle. Maybe I didn't explain it clearly. It's probably like this. Readers can test it by themselves. As for marquee, it's the text walking lantern effect.

Of course, if you only set these words, you won't scroll. Also make textview get focus.

Focusable: whether it can focus, boolean type

Focusableintouchmode: Boolean type.

Whether to obtain focus in touch mode.

When you set up these deployment on the mobile phone, it will obviously achieve the riding light effect. The effects are as follows:

However, if you add another edit box control to this activity instance, click the edit box and you will find that the riding light effect disappears

Like this

Why?

Because when you click the edit box, the edit box will get the screen focus. Generally, there can only be one focus on the screen. If textview loses the focus, it will not scroll. What shall we do at this time?

Then cheat the system. Tell it that our textview also has focus. Yes, two focuses.

How? Let's just create our own textview.

First, we create a new class named mytextview, which inherits textview, and override the methods in it. Three methods are required, just as we always override oncreate method in mainactivity. I don't know what the function is. Curious students please Baidu. Ha ha~

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

That's the way. As mentioned earlier, we want to cheat the system. Our textviwe has a focus. So we should always return true in this method. Haha, are you a hooligan...

After that, don't forget to deploy our textview to the layout file

Layout code:

MainActivity.class

MyTextView.class

By the way, attach the renderings. You can see the cursor in the edit box~

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>