The selector in Android is primarily used to change the default background for other nulls, such as the ListView and button controls, which can be used as follows:
1, first in the Res Directory drawable folder, create a new comm_button_bg.xml XML file.
2. Edit the Comm_button_bg.xml file, and its code structure is as follows:
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"
<!--the background picture when clicked in touch mode--
<Itemandroid:drawable= "@drawable/btn_bg_pressed"android:state_pressed= "true"></Item>
<Itemandroid:drawable= "@drawable/btn_bg_normal"android:state_pressed= "false"></Item> <!--the background picture when you get focus in non-touch mode and click
</selector>
3. You can customize the style you want according to your project needs, as follows:
sets the selection button pressed and not pressed text color changes effect TextColor<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"><!--Set the text color of the control when clicked,---><ItemAndroid:color= "@android: Color/white"android:state_pressed= "true"></Item> <ItemAndroid:color= "@android: Color/black"android:state_pressed= "false"></Item></selector>
A similar attribute can also be:
Android:state_selected selected
Android:state_focused Get Focus
android:state_pressed Click
Android:state_enabled setting whether to respond to an event, refers to all events
4. Use the Comm_button_bg.xml defined style in the layout file
< Button Android:background = "@drawable/common_button_bg" <!----use a custom style-----> android:text= "Test" android:textcolor= "@drawable/common_button_textcolor_selector" android:textsize= "20SP"/ >
Summary of the use of selector in Android