Sometimes we have to implement the "push button and Release button, button background picture (color)" effect, we can use selector to achieve
1. Write the XML code:
Create a new XML file in the project's res/drawable directory and add the relevant code:
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:state_pressed= "true"android:drawable= "@drawable/button_pressed" /> <!--press the background image after - <Itemandroid:drawable= "@drawable/button_normal" /> <!--The default background picture -</selector>
View Code
Of course, in this directory to put the relevant pictures.
Each attribute explains:
Whether the android:state_pressed is pressed, such as a button touch or click.
Whether the android:state_focused has the focus, such as when a user selects a text box.
Android:state_hovered the cursor is hovering, usually the same as focused state, which is a new feature of 4.0
Android:state_selected is selected, and it is not exactly the same as the focus state, when a list view is selected, the various sub-components within it may be selected by the arrow keys.
Whether the Android:state_checkable component can be check. For example: RadioButton can be check.
Android:state_checked was checked, such as: A RadioButton can be check.
Android:state_enabled can accept touch or click events
Android:state_activated is activated the android:state_window_focused application is in the foreground and the application is not in the foreground when a notification bar is pulled down or a dialog box pops up
2. In the layout file of the button that you want to set this effect to, add:
< Button Android:layout_height = "Wrap_content" android:layout_width= "Wrap_content" android:background= "@drawable/ Button "/>
Android Click button to toggle background effect-selector How to use