This article describes the implementation method of dynamic buttons for Android programming. Share it for your reference, as follows:
The first type:This method is implemented through onTouch,
btn3 = (ImageButton) findViewById(.ImageButton03); (touchListener3); touchListener = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { ImageButton imageBtn = (ImageButton) v; if(() == MotionEvent.ACTION_DOWN){ //Change to background image when pressedimageBtn .setImageResource(); }else if(() == MotionEvent.ACTION_UP){ //Replace the picture when liftedimageBtn .setImageResource(); } return false; } };
The second type:Implemented through XML
Implemented with XML files:
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:andro> <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 in the drawable directory. Named button_add_x.xml
When using:
<ImageButton android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:src="@drawable/button_add_x" > </ImageButton>
For more information about Android related content, please check out the topic of this site:Android layout layout tips summary》、《Android View View Tips Summary》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Summary of the usage of basic Android components"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.