Android Text Status selector-colorstatelist

Source: Internet
Author: User

Preface: We all know that in drawable we can customize the selection state of other controls such as button, such as what color to select, what color is the default, but the button's text can also be set in this way

, this requires the Colorstatelist class.

1. This is his document.

2. How to use

① Similar to the need to define an XML selector, but the item only needs to indicate the state and color

button_text_selector.xml File

1 <selectorxmlns:android= "Http://schemas.android.com/apk/res/android">2 3     <Itemandroid:state_pressed= "true"Android:color= "#ffff0000"/>4  <!--pressed -5     <Itemandroid:state_focused= "true"Android:color= "#ff0000ff"/>6  <!--focused -7     <ItemAndroid:color= "#ff000000"/>8  <!--default -9 Ten </selector>

② is called in Java code in two different ways

1Button BTN =(Button) Findviewbyid (R.ID.ID_BTN);2         //The first way3         //Import selection XML for text4Xmlpullparser XRP =getresources (). GETXML (5 r.drawable.button_text_selector);6         Try {7Colorstatelist CSL =Colorstatelist.createfromxml (Getresources (),8 XRP);9 Btn.settextcolor (CSL);Ten}Catch(Exception e) { One  A         } -         //The second way -Resources Resources =getresources (); theColorstatelist CSL =Resources - . Getcolorstatelist (r.drawable.button_text_selector); -  -         if(CSL! =NULL) { + Btn.settextcolor (CSL); -}

3. Effects

Android Text Status selector-colorstatelist

Related Article

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.