Android spannablestring Basic Usage

Source: Internet
Author: User

The following is a description of some of Spannablestring's special handling of text: fonts, colors, underscores, links, and click events.
First look at the effect:

Layout code is not affixed, very simple is 3 textview. Now look at the code implementation in Mainactivity:

    PrivateTextView MTvContent1;PrivateTextView MTvContent2;PrivateTextView MTvContent3;PrivateString CONTENTSTR1 ="Click events, Underline, font color";PrivateString CONTENTSTR2 ="Baidu Link";PrivateString CONTENTSTR3 ="Bold";@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Set Click events, underline, font colorMTvContent1 = (TextView) Findviewbyid (R.ID.TV_CONTENT1); Spannablestring SS1 =NewSpannablestring (CONTENTSTR1); Ss1.setspan (NewClickablespan () {@Override             Public void updatedrawstate(Textpaint DS) {Super. Updatedrawstate (DS); Ds.setunderlinetext (true);//Set underlineDs.setcolor (color.red);//Set Font color}@Override             Public void OnClick(View widget) {Toast toast = Toast.maketext (mainactivity. This,"click", Toast.length_short);            Toast.show (); }        },0, Contentstr1.length (), spanned.span_exclusive_exclusive);        Mtvcontent1.settext (SS1);        Mtvcontent1.sethighlightcolor (color.transparent); Mtvcontent1.setmovementmethod (Linkmovementmethod.getinstance ());//Settings linkMtvcontent2= (TextView) Findviewbyid (R.id.tv_content2); Spannablestring SS2 =NewSpannablestring (CONTENTSTR2); Ss2.setspan (NewUrlspan ("Http://www.baidu.com"),0, Contentstr2.length (), spanned.span_exclusive_exclusive);        Mtvcontent2.settext (SS2); Mtvcontent2.setmovementmethod (Linkmovementmethod.getinstance ());//Set Bold:mtvcontent3= (TextView) Findviewbyid (R.ID.TV_CONTENT3); Spannablestring SS3 =NewSpannablestring (CONTENTSTR3); Ss3.setspan (NewStylespan (Android.graphics.Typeface.BOLD),0, Contentstr3.length (), spanned.span_exclusive_exclusive);    Mtvcontent3.settext (SS3); }}

This is the entire code: where the click and link events, pay special attention to the need to setmovementmethod, otherwise it may not respond to the event.

Android spannablestring Basic Usage

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.