ClickableSpan: implements a click response for a part of TextView text. textfield clicks do not respond.
Hypertext: http://www.baidu.com
This effect: a line of text in front of the black color "hypertext:", followed by a red color "http://www.baidu.com" and asked the red font part can be clicked, how do you implement response-oriented click events?
The simplest way to achieve this is two textviews, the first one showing the black font of the "hypertext", the second showing the red font of the "http://www.baidu.com", then adding a click event to the second TextView.
Think about it. It can be implemented in this way.
However, if the text length is too long, the text that fails to be displayed in the second TextView will wrap, but it is not at the beginning of the second line.
What you want is
Bytes ----------------------------------------------------------------------------------------------
Obviously, it is difficult to use two textviews. If you use a TextView to implement a paragraph of text, you can have different colors or click events for different sections of text?
First, we must know that the SpannableString class can realize different colors for different parts of the same TextView,
No, you can go to the following link: () SpannableString and SpannableStringBuilder
Now that we know how to implement a TextView to display different colors, there is another question: how to click the text in different parts of the same TextView for corresponding response operations
Learn now:
ClickableSpan
The source code is very short and can be directly posted. Let's take a look at it with my personal Translation:
/*** If an object of this type is attached to the text of a TextView * with a movement method of LinkMovementMethod, the affected spans of * text can be selected. if clicked, the {@ link # onClick} method will * be called.
If this TextView is used. setMovementMethod () method, the text part of setSpan () can be selected. If you click it, onClick () is executed () interface callback Method */public abstract class ClickableSpan extends CharacterStyle implements UpdateAppearance {/*** Performs the click action associated with this span. */public abstract void onClick (View widget);/*** Makes the text underlined and in the link color. * // @ Override public void updateDrawState (TextPaint ds) {ds. setColor (ds. linkColor); // you can click the color ds in the text area. setUnderlineText (true); // sets whether the text part displays an underline in hyperlink format }}
I may not understand it, but I don't need to worry about it. How can I use it?
1. First, the source code says:
If an object of this type is attached to the text of a TextView * with a movement method of LinkMovementMethod,
So:
For a TextView, you must first use TextView. setMovementMethod (LinkMovementMethod. getInstance ());
How can I set this attribute for a TextView? In fact, this TextView is used to achieve the hyperlink effect. If this parameter is not set, no click event is required.
2. The source code also says:
the affected spans of * text can be selected. If clicked, the {@link #onClick} method will * be called.
The text in setSpan () can be displayed, and The OnClick () interface callback can be implemented when you click it.
So you need to write a class to implement ClickableSpan
And. setSpan (inheriting the class of ClickableSpan, corresponding to the start position of the effect, corresponding to the end position of the effect, Spanned. SPAN_EXCLUSIVE_EXCLUSIVE );
Bytes ----------------------------------------------------------------------------------------------
Let's use a Demo to learn how to use ClickableSpan:
1. Select a custom class to inherit from ClickableSpan.
Class MyClickText extends ClickableSpan {private Context context; public MyClickText (Context context) {this. context = context;} @ Override public void updateDrawState (TextPaint ds) {super. updateDrawState (ds); // sets the text color ds. setColor (Color. RED); // underline in the form of hyperlink. false indicates that the underline is not displayed, and true indicates that the underline ds is displayed. setUnderlineText (false) ;}@ Override public void onClick (View widget) {Toast. makeText (context, "click effect occurred", Toast. LENGTH_SHORT ). show ();}}
2. Operate TextView
Private TextView clicktext; clicktext = (TextView) findViewById (R. id. clicktext); SpannableString str = new SpannableString ("hypertext: http://www.baidu.com"); str. setSpan (new MyClickText (this), 4, str. length (), Spanned. SPAN_EXCLUSIVE_EXCLUSIVE); // Of course, you can use setSpan to set the position of the text and the color of clicktext. setText (str); clicktext. setMovementMethod (LinkMovementMethod. getInstance (); // The clicktext event is not set. setHighlightColor (Color. TRANSPARENT); // set the color after clicking to TRANSPARENT
As you can see, I feel like I have a long knowledge, but I think about the actual use of this class?
In social apps, functions similar to QQ space are generally used. A page displays all the dynamics in the form of a list, and each dynamic one corresponds to the corresponding comments.
Can we use the ClickableSpan class to display text of different colors in a TextView and click the corresponding position to display the corresponding Event Response Results?
The problem arises. In the Demo, we determine where a TextView is colored or click events, but in the actual project, we are not sure about the length of the nickname of the reviewer and the user to be commented on. What should we do? The next blog will introduce it.