Add multiple clickable texts for TextView in Android, androidtextview

Source: Internet
Author: User

Add multiple clickable texts for TextView in Android, androidtextview


When we use social media software, we may like other people's posts more or less ,:


You can see that the user page displays only the names of the users who liked the user. Click these names to go to the user's home page. We will achieve similar results. Run the Code directly.

@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TextView mTextView = (TextView) rootView. findViewById (R. id. like_ TV); // construct the html of multiple hyperlinks and obtain the username StringBuilder sbBuilder = new StringBuilder (); for (int I = 0; I <10; I ++) {sbBuilder. append ("username-" + I + ",");} String likeUsers = sbBuilder. substring (0, sbBuilder. lastIndexOf (",")). toString (); mTextView. setMovementMethod (LinkMovementMethod. getInstance (); mTextView. setText (addClickablePart (likeUsers), BufferType. SPANNABLE);}/*** @ param str * @ return */private SpannableStringBuilder addClickablePart (String str) {// The first like icon ImageSpan span = new ImageSpan (getActivity (), r. drawable. umeng_comm_like); SpannableString spanStr = new SpannableString ("p. "); spanStr. setSpan (span, 0, 1, Spannable. SPAN_INCLUSIVE_EXCLUSIVE); SpannableStringBuilder ssb = new SpannableStringBuilder (spanStr); ssb. append (str); String [] likeUsers = str. split (","); if (likeUsers. length> 0) {// The last for (int I = 0; I <likeUsers. length; I ++) {final String name = likeUsers [I]; final int start = str. indexOf (name) + spanStr. length (); ssb. setSpan (new ClickableSpan () {@ Override public void onClick (View widget) {Toast. makeText (getView (). getContext (), name, Toast. LENGTH_SHORT ). show () ;}@ Override public void updateDrawState (TextPaint ds) {super. updateDrawState (ds); // ds. setColor (Color. RED); // set the text color // remove the underline ds. setUnderlineText (false) ;}}, start, start + name. length (), 0) ;}} return ssb. append ("" + likeUsers. length + "I personally liked you. ");} // end of addClickablePart
Running effect:





How to add multiple links to a TextView (some text can be clicked)

Android uses the Spannable feature to set strings in segments. It uses the modifier mode and can be used together by multiple Spannable
 
Android adds a text component. TextView txt = new TextView (this); is it defined + instantiated?

1. Yes
2. No
3. If only the component is created, the same name of the component in the same scope will not be affected. What are specifications? The rules are determined by the dead.
4 ,,,,,

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.