Android TextView add Clickablespan Click to select text Background question

Source: Internet
Author: User

The sethighlightcolor (int color) in TextView is used to set the highlight of the selected text background color.

such as the following:

 Public classMainactivityextendsactionbaractivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Framelayout Framelayout=NewFramelayout ( This);        Setcontentview (framelayout);        Framelayout.setid (R.id.container); if(Savedinstancestate = =NULL) {Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.container,Newplaceholderfragment ()). commit (); }} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.menu_main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); //noinspection simplifiableifstatement        if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }    /*** A placeholder fragment containing a simple view. */     Public Static classPlaceholderfragmentextendsFragment { Publicplaceholderfragment () {} @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstance State) {View Rootview= Inflater.inflate (R.layout.fragment_main, container,false); returnRootview; } @Override Public voidonviewcreated (view view, @Nullable Bundle savedinstancestate) {Super. onviewcreated (view, savedinstancestate); TextView TextView=(TextView) View.findviewbyid (R.id.textview); String Str= "click me!"; String txt= str + "Hello world!"; Spannablestring spannablestring=Newspannablestring (TXT); Clickablespan Clickablespan=NewClickablespan () {@Override Public voidOnClick (View widget) {//Do something.                    if(isadded ()) {Toast.maketext (), getactivity (),"You have clicked!", Toast.length_long). Show ();//Avoidhintcolor (widgets);}} @Override Public voidupdatedrawstate (@NonNull textpaint ds) {Super. Updatedrawstate (DS); Ds.setcolor (Getresources (). GetColor (Android.                    R.color.holo_red_dark)); Ds.setunderlinetext (false);                Ds.clearshadowlayer ();            }            }; Spannablestring.setspan (Clickablespan,0, Str.length (), spanned.span_exclusive_exclusive);            Textview.settext (spannablestring);        Textview.setmovementmethod (Linkmovementmethod.getinstance ()); }        Private voidAvoidhintcolor (view view) {if(ViewinstanceofTextView) ((TextView) view). Sethighlightcolor (Getresources (). GetColor (Android.        R.color.transparent)); }    }}        

A pale green background color appears when text is selected. such as 1.1. Ds.setcolor () sets the text color of the span hyperlink, not the clicked color, the background color after the click (HighlightColor) belongs to the TextView property, Android4.0 the above default is light green, the lower version is yellow.

The solution is through

((TextView) view). Sethighlightcolor (Getresources (). GetColor (Android. R.color.transparent)); method re-set the text background as transparent color.

The modified result 1.2.

Figure 1.1:

Figure 1.2:

Android TextView add Clickablespan Click to select text Background question

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.