Configured in Drawable/xxx.xml, by configuring selector, you can make the system run with appropriate pictures, text, and so on, based on the state of the control object.
- android:state_selected control checked state, can be true or false
- android:state_focused Control Gets the focus state, can be true or false
- android:state_pressed control click State, can be true or false
- android:state_ Enabled control enable state, can be true or false
- android:state_checkable control can be checked for a status of true or false
- android:state_checked Control check state, can be true or false
Note: in the status description, the first item that matches the current state is used. Therefore, if the first item does not have any state attributes, it will be used every time, so the default value must always be the last.
- android:window_focused application window Focus State, can be true or false
- android:color defines a color of 16 for a particular state. This color is specified by the RGB value, with Alpha, must start with "#", followed by Alpha-red-green-blue information, in the format:#rgb,#argb,# RRGGBB,#aarrggbb
Example 01:
<?XML version= "1.0" encoding= "Utf-8"?> <selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <!--background picture by default - <Itemandroid:drawable= "@drawable/pic1" /> <!--background picture When there is no focus - <Itemandroid:state_window_focused= "false"android:drawable= "@drawable/pic1" /> <!--background picture When you get focus in non-touch mode and click - <Itemandroid:state_focused= "true"android:state_pressed= "true"android:drawable= "@drawable/pic2" /> <!--background picture when clicked in touch mode - <Itemandroid:state_focused= "false"android:state_pressed= "true"android:drawable= "@drawable/pic3" /> <!--picture Background When selected - <Itemandroid:state_selected= "true"android:drawable= "@drawable/pic4" /> <!--picture background when getting focus - <Itemandroid:state_focused= "true"android:drawable= "@drawable/pic5" /> </selector>
Example 02:
Give Button a different state when you press or get focus
<?XML version= "1.0" encoding= "Utf-8"?> <selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:state_pressed= "true"android:drawable= "@drawable/login_button2" /> <Itemandroid:state_focused= "true"android:drawable= "@drawable/login_button2" /> <Itemandroid:drawable= "@drawable/login_button" /> </selector>
How to use:
< Button Android:id = "@+id/style_button_id" android:layout_width= "Wrap_content" android:layout_height= "Wrap_ Content " android:layout_gravity=" center " android:layout_margintop = "16DP" android:text= "Dynamically set click Style with code"/>
Reference from:
http://blog.csdn.net/breeze666/article/details/7747649
Http://www.cnblogs.com/tianzhijiexian/p/3854962.html
Selector in Android