In addition to the button buttons on the Android system, buttons with icons are available ImageButton
To make a button with an icon, you first define the ImageButton in the layout file and then set the icon to be displayed by using the Setimagedrawable method.
Attention:
We can set the icon of the button directly in the layout file, such as
android:src= "@drawable/icon1″
We can also set the custom icon in the program
Imgbtn3.setimagedrawable (Getresources (). getdrawable (R.drawable.icon2));
We can also use the system's own icon
Imgbtn4.setimagedrawable (Getresources (). Getdrawable (Android. r.drawable.sym_call_incoming));
After setting the icon for the button, you need to set the listener Setonclicklistener for the button to capture the event and handle
The following example is a layout composed of 4 icon buttons, of which three button icons are custom, the fourth button icon is the system, when clicked Button 1, Pop dialog, when Clicked Button 2, click OK, you can turn the button 2 icon button 3 icon, When the button 3 is clicked, the button 3 icon becomes the system call icon, click the button 4, display a hint dialog
Imagebuttontest.java Source Code
Package Org.loulijun.imagebutton;import Android.app.activity;import Android.app.alertdialog;import Android.app.dialog;import Android.content.dialoginterface;import Android.os.bundle;import Android.view.View; Import Android.widget.button;import Android.widget.imagebutton;import Android.widget.textview;public class Imagebuttontest extends activity {/** Called when the activity is first created. */TextView TextView; ImageButton imgbtn1; ImageButton imgbtn2; ImageButton Imgbtn3; ImageButton Imgbtn4; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); textview= (TextView) Findviewbyid (R.id.textview); 4 ImageButton Objects imgbtn1= (ImageButton) Findviewbyid (R.id.imagebutton1) were obtained. Imgbtn2= (ImageButton) Findviewbyid (R.id.imagebutton2); imgbtn3= (ImageButton) Findviewbyid (R.id.imagebutton3); imgbtn4= (ImageButton) Findviewbyid (r.id.IMAGEBUTTON4); The ImageButton setting icon//IMGBTN1 is already set in the Main.xml layout, so it is not set here (the icon can be set in the program or set in the layout file) imgbtn2.setimagedrawab Le (Getresources (). getdrawable (R.drawable.icon));//Set the icon imgbtn3.setimagedrawable (Getresources () in the program. Getdrawable ( R.drawable.icon2)); Imgbtn4.setimagedrawable (Getresources (). Getdrawable (Android. r.drawable.sym_call_incoming));//Set the System icon//Set event listener Imgbtn1.setonclicklistener for each button below (new Button.onclicklistene R () {@Override public void OnClick (View v) { TODO auto-generated Method Stub Dialog dialog=new alertdialog.builder (imagebutto ntest.this). Settitle ("hint"). Setmessage ("I am ImageButton1") . Setpositivebutton ("OK", new Dialoginterface.onclicklistener () { @Override public void OnClick (Dialoginterface dialog, int which) { TODO auto-generated Method Stub//corresponding processing operation }}). Create (); Dialog.show (); } }); Imgbtn2.setonclicklistener (New Button.onclicklistener () {@Override P ublic void OnClick (View v) {//TODO auto-generated method stub Dialog dialog=new Alertdialog.builder (imagebuttontest.this). Settitle ("hint") . Setmessage ("I am ImageButton2, I want to use the ImageButton3 icon"). Setpositivebutton (" New Dialoginterface.onclicklistener () {@Override public void OnClick (Dialoginterface dialog, int which) {//TODO Aut o-generated method Stub imgbtn2.setimagedrawable (Getresources (). getdrawable (R.drawable.icon2)); }}). Create (); Dialog.show (); } }); Imgbtn3.setonclicklistener (New Button.onclicklistener () {@Override P ublic void OnClick (View v) {//TODO auto-generated method stub Dialog dialog=new Alertdialog.builder (imagebuttontest.this). Settitle ("hint") . Setmessage ("I am ImageButton3, I want to use the system call icon"). Setpositivebutton ("OK", new Dialoginterface.onclicklistener () { @Override public void OnClick (Dialoginterface dialog, int which) { TODO auto-generated Method Stub imgbtn3 . Setimagedrawable (Getresources (). Getdrawable (Android. R.drawable.sym_action_call)); }}). Create (); Dialog.show (); } }); Imgbtn4.setonclicklistener (New Button.onclicklistener () {@Override P ublic void OnClick (View v) {//TODO auto-generated method stub Dialog dialog=new Alertdialog.builder (imagebuttontest.this). Settitle ("hint") . Setmessage ("I am using the System icon"). Setpositivebutton ("OK", new Dialoginterface.on ClicklistenER () {@Override public void OnClick (Dialogi Nterface dialog, int which) {//TODO auto-generated method stub The corresponding processing operation}). Create (); Dialog.show (); } }); }}
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:id= "@+id/textview" android:layout_width= "fill_parent" android:layout_height= "Wrap_c Ontent "android:text=" ImageButton test Case "/><imagebutton android:id=" @+id/imagebutton1 "Android:lay Out_width= "Wrap_content" android:layout_height= "wrap_content" android:src= "@drawable/icon1"/><imagebut ton android:id= "@+id/imagebutton2" android:layout_width= "wrap_content" android:layout_height= "Wrap_co Ntent "/><imagebutton android:id=" @+id/imagebutton3 "android:layout_width=" Wrap_content "Android : layout_height= "wrap_content"/><imagebutton android:id= "@+id/imagebutton4" android:layout_width= "Wrap_ Content "Android:layout_height= "Wrap_content"/></linearlayout>
The results are as follows:
After clicking the first button
Click OK, then click the second button
Click OK and you'll see the icon for button two is programmed to be the same as the icon for button three.
Click on the button three
When you click OK, the icon for the button three becomes the icon of the system call.
Click Button Four
The above is the Android UI Control series: ImageButton (button with icons), more about topic.alibabacloud.com (www.php.cn)!