Android for TextView add multiple click Text

Source: Internet
Author: User


We use social software in the process will be more or less like other people's posts point, figure. :


To be able to see the user page is just the name of the user who likes to click on the name, clicking on these names to access the user's home page. We're going to achieve similar results. 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 < 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 (str            ing str) {//First likes 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) {//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//To drop the dash Ds.setunderlinetext (false);                }}, start, start + name.length (), 0);        }} return Ssb.append ("et" + likeusers.length + "personally liked you."); }//End of Addclickablepart
Execution effect:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvymjvewzlaxl1/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "width=" height= "640" style= "border:1px solid black"/>



Copyright notice: This article mr.simple original article, must not be reproduced without consent.

Android for TextView add multiple click Text

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.