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