Add multiple clickable text for TextView in Android

Source: Internet
Author: User

The way we use social software is more or less like other people's posts:

You can see that the user page shows just the name of the user who likes it, and clicking on those names can go to the user's homepage. We're going to do something like that. Go directly to the code.

    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  Setcontentview (R.layout.activity_main);            TextView Mtextview = (TextView) Rootview.findviewbyid (R.ID.LIKE_TV);            Constructs multiple hyperlinks of HTML, with the selected location to get the user name StringBuilder Sbbuilder = new StringBuilder ();            for (int i = 0; i < 5; i++) {sbbuilder.append ("username-" + i + ",");            The String likeusers = sbbuilder.substring (0, Sbbuilder.lastindexof (",")). ToString ();            Mtextview.setmovementmethod (Linkmovementmethod.getinstance ()); Mtextview.settext (Addclickablepart (likeusers), buffertype.spannable); }/** * @param str * @return */private Spannablestringbuilder Addclickablepart (stri            ng str) {Spannablestringbuilder SSB = new Spannablestringbuilder (str);            string[] Likeusers = Str.split (",");               if (Likeusers.length > 0) { Last for (int i = 0; i < likeusers.length; i++) {final String name = Likeusers[i                    ];                    Final int start = Str.indexof (name);  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);                        Remove the underscore ds.setunderlinetext (false);                }}, start, start + name.length (), 0);        }} return Ssb.append ("et" + likeusers.length + "personally liked you."); }//End of Addclickablepart
Operating effect:



Add multiple clickable text for TextView in Android

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.