The URL in textview specifies the special string and click event resolution

When using textview, you may need to give a special style to specific strings in textview, such as URLs and numbers. You must want to add click events. For example, when sending text messages, you can click the URL in the text to directly open the browser, and you can click the number to make a call.

Android provides spannablestring class to process the content of textview. The specific steps are as follows:

1. New is a spannablestring object that passes in the content to be displayed on the textview;

2. Process the content (such as parsing URLs or numbers with regular expressions and specifying styles). The core of this part is to call the setspan () method of spannablestring object;

You can see that there are four parameters. Start and end mean that you want to change 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 something you specify to replace the text, such as a picture. There are four kinds of flags, which means whether to affect the previous or next string at the specified place

3. Textview. Settext (spannablestring). Because spannablestring implements charsequence, textview can set directly.

Note: the specified content of textview has already implemented the specified style., However, if you want to add a click event, you also need to make textview call setmovementmethod (movementmethod movement) to implement the click event.

My demo is posted below to mark the numbers in textview. Click the numbers to pop up toast to display the numbers.

The code is very simple. The first thing to say is clickablespan, because when you click the specific string, the ontouchevent () method of linkmovementmethod will obtain the clickablespan object according to the location you click, and execute the onclick method of the object. This clickablespan needs to be passed in during setspan (this place can be passed in not only clickablespan, but also imagespan. However, 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, including the front and excluding the back

Spanned.SPAN_ EXCLUSIVE_ Exclusive is not included before or after the current setting

Spanned.SPAN_ INCLUSIVE _ Inclusive current settings include front and back

Spanned.SPAN_ EXCLUSIVE_ Inclusive the current setting does not include the front and includes the back

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support 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
分享
二维码
< <上一篇
下一篇>>