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 addClickablePartRunning 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 ,,,,,