Many people have said that the button cannot use a custom click or select effect. Therefore, perform the following test:
The result shows that the button is very powerful.
You can use this flexible method.
Method 1
1. the selector. xml file placed under drawable
<? XML version = "1.0" encoding = "UTF-8"?>
<Selector xmlns: Android = "http://schemas.android.com/apk/res/android">
<Item Android: state_pressed = "true" Android: drawable = "@ drawable/temp1"/>
<Item Android: state_pressed = "false" Android: state_focused = "false"
Android: drawable = "@ drawable/temp2"/>
<Item Android: state_focused = "true" Android: drawable = "@ drawable/temp3"/>
<Item Android: state_focused = "false" Android: drawable = "@ drawable/temp4"/>
</Selector>
2. layout file main. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Button
Android: drawabletop = "@ drawable/shouru"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/button"
Android: Background = "@ drawable/selector"/>
</Linearlayout>
3. It is only for testing, so the effect is not very good. The status of the button is as follows:
① The default button effect displayed during initialization:
② Click to release the Display Effect
③ Effect of clicking the button
Method 2
1. layout file main. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
<Button
Android: Id = "@ + ID/button"
Android: drawabletop = "@ drawable/shouru"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/button"
Android: Background = "@ drawable/temp4"/>
</Linearlayout>
2. Main Java code to achieve click effect:
Button button = (button) This. findviewbyid (R. Id. Button );
Button. setontouchlistener (New button. ontouchlistener (){
@ Override
Public Boolean ontouch (view V, motionevent event ){
If (event. getaction () = motionevent. action_down ){
V. setbackgroundresource (R. drawable. temp1 );
Log. I ("testandroid button", "motionevent. action_down ");
}
Else if (event. getaction () = motionevent. action_up ){
V. setbackgroundresource (R. drawable. temp2 );
Log. I ("testandroid button", "motionevent. action_up ");
}
Return false;
}
});
This method achieves the same effect as above. You can choose one based on your actual situation.