Generally, you can use imagebutton to display a button. However, sometimes we want to change the button status, for example, before the button is pressed, after the button is pressed, it is another way. Android allows us to change the button image depending on different states, if the button is set or the button is pressed. The following describes how to implement it:
P.s this project is developed in eclipse 3.7, and tested with Android 2.3.3.
1. Add an image to resources to prepare three images with different button states, and then put the image
"
Resource/drawable"
.
- Button_normal_green.png-Default image button.
- Button_focused_orange.png-
When a button is followed, for example, it is displayed when the phone keyboard moves (Focus.
- Button_pressed_yellow.png-
Displayed when the button is pressed
2. Add selector for different button States and create a new XML layout file in "Res/drawable/". Here we name it"
new_button.xml
". This XML file defines which button image the button status belongs.
File: Res/drawable/new_button.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/button_pressed_yellow" android:state_pressed="true" /> <item android:drawable="@drawable/button_focused_orange" android:state_focused="true" /> <item android:drawable="@drawable/button_normal_green" /></selector>
3. Add button
Open the "Res/layout/Main. xml" layout file, add a normal button, and then add a background image for this button.
File
: Res/layout/Main. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <button Android: id = "@ + ID/imagebuttonselector" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: background = "@ drawable/new_button"/> // This method is used to implement </linearlayout>
4. the activity code is like a normal button and a simple click listener package com. Demo. app;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Import Android. widget. Toast;
Public class apptestactivity extends activity {
/* (Non-javadoc)
* @ See Android. App. Activity # oncreate (Android. OS. Bundle)
*/
Button imagebutton;
@ Override
Protected void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Imagebutton = (button) findviewbyid (R. Id. imagebuttonselector );
Imagebutton. setonclicklistener (New onclicklistener (){
Public void onclick (view v ){
Toast. maketext (apptestactivity. This, "eee", Toast. length_short );
}
});
}
}
5. Run
Run the application
1. Result: The default button. (button_normal_green.png)
2.
Button focus. (button_focused_orange.png)
2.
Button is pressed. (button_focused_orange.png)
This application is still very extensive, such as player control and game status.