This example shows a way to add multiple clickable text to TextView in Android. This feature is very useful in the production of Android social software. Share to everyone for your reference. Specifically as follows:
Most of the time we use social software in the process of more or less for others to praise the post, as shown in the following figure:
You can see that the user page displays just the name of the user who clicked on it, and clicking on the name can go to the user's homepage. Here's what we can do to achieve a similar effect.
The specific code is as follows:
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
TextView Mtextview = (TextView) Rootview.findviewbyid (R.ID.LIKE_TV);
Constructs the HTML of multiple hyperlinks and obtains the username StringBuilder sbbuilder = new StringBuilder () by the selected location.
for (int i = 0; i < 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) {//First praise icon Imagesp
An 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 = Li
Keusers[i];
Final int start = Str.indexof (name) + spanstr.length (); Ssb.setspan (New Clickablespan () {@Override public void OnClick (View widget) {toast.maketext (GetView ().
Context (), name, Toast.length_short. Show ();
@Override public void Updatedrawstate (Textpaint ds) {super.updatedrawstate (DS); Ds.setcolor (color.red);
Set the color of the text//To drop the underlined ds.setunderlinetext (false);
}, start, start + name.length (), 0);
} return Ssb.append ("Wait" + Likeusers.length + "personally praised you.");
}//End of Addclickablepart
The effect is as shown in the following illustration:
I hope this article will help you with your Android program.