When using TextView, you may sometimes need to give specific strings in the TextView, such as URLs or numbers with special styles, and you must hope to add click events. For example, when sending a text message, you can click the URL in the text to open the browser directly, and click to make a call.
Android provides the SpannableString class to process the content of the TextView. The specific steps are:
A SpannableString object passing in the content that needs to be displayed on the TextView;
2. Process the content (such as using regular expressions to parse URLs or numbers, etc., and specify styles). The core of this part is to call the setSpan() method of the SpannableString object;
public void setSpan(Object what, int start, int end, int flags);
You can see that there are four parameters, start and end means that what you want to change is the part of the string subscript start to end. What can be passed in the processing method or style of the text from start to end, or you can pass in the thing you specified to replace the text, such as an image. There are four types of flags in total, which means whether it affects the previous or next string at the specified point.
(spannableString), Since SpannableString implements CharSequence, TextView can be set directly.
Notice:At this point, the specified content of TextView has already implemented the specified style. , but if you want to add a click event, you also need to let the TextView call setMovementMethod(MovementMethod movement) to implement the click event.
My demo is posted below to mark the numbers in the TextView. Click the number to pop up Toast to display the numbers.
private void init() { (()); SpannableString s = new SpannableString(CONTENT); filterNumber(s); (s); } private static final String REG = "\\d+"; public class TextClickableSpan extends ClickableSpan { private String text; public TextClickableSpan(String text) { = text; } @Override public void onClick(View view) { (,text,Toast.LENGTH_SHORT).show(); } } private void filterNumber(Spannable s) { Matcher m = (REG).matcher(()); while (()) { String text = (); TextClickableSpan span = new TextClickableSpan(text); (span,(),(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } }
The code is very simple. The first thing to talk about is ClickableSpan, because when you click on that specific string, the onTouchEvent() method of LinkMovementMethod will obtain the ClickableSpan object according to the position you clicked, and execute the onClick method of the object. This ClickableSpan needs to be passed in when setSpan (this place can not only pass in ClickableSpan, but also in ImageSpan, etc., but if you want to click, you need to pass in ClickableSpan. Others will not be described here).
Then my filterNumber method uses regular expressions to filter numbers and calls setSpan to pass in the corresponding start and end one by one.
Finally, let’s talk about the last parameter flags of setSpan:
Spanned.SPAN_INCLUSIVE_EXCLUSIVE is the current setting that includes the front and does not include the back.
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE The current settings are not included before and after
Spanned.SPAN_INCLUSIVE _INCLUSIVE The current setting includes the front and the back
Spanned.SPAN_EXCLUSIVE_INCLUSIVE The current setting does not include the front, the back
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.