The example of this article describes the implementation of the Android programming dynamic button. Share to everyone for your reference, specific as follows:
The First: The method is implemented through Ontouch,
Btn3 = (ImageButton) Findviewbyid (r.id.imagebutton03);
Btn3.setontouchlistener (TOUCHLISTENER3);
View.ontouchlistener Touchlistener = new Ontouchlistener () {
@Override public
boolean Ontouch (View V, Motionevent event) {
ImageButton imagebtn = (ImageButton) v;
if (event.getaction () = = Motionevent.action_down) {
//change to the background picture when pressed
imagebtn. Setimageresource ( r.drawable.pressed);
} else if (event.getaction () = = motionevent.action_up) {
//////////
IMAGEBTN r.drawable.released);
}
return false;
}
;
The second way: through XML to implement
Implemented in XML file:
<?xml version= "1.0" encoding= "UTF-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<item
android:state_pressed= false"
android:drawable= "@drawable/button_add"/>
<item
android:state_pressed= "true"
android:drawable= "@drawable/button_add_pressed"/>
<item
android:state_focused= "true"
android:drawable= "@drawable/button_add_pressed"/>
< Item
android:drawable= "@drawable/button_add"/>
</selector>
This file is placed under the drawable directory. Named Button_add_x.xml
When in use:
<imagebutton
android:id= "@+id/imagebutton"
android:layout_width= "Wrap_content"
android: layout_height= "Wrap_content"
android:background= "#00000000"
android:src= "@drawable/button_add_x"
>
</ImageButton>
More interested readers of Android-related content can view this site: "Android Layout Layout Skills Summary", "Android View Overview", "Android Development introduction and Advanced Course", " Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary" and "Android Control usage Summary"
I hope this article will help you with the Android program.