To create a button with an icon, first define ImageButton in the layout file, and then set the icon to be displayed using the setImageDrawable method. In addition to the buttons in the Android system, the ImageButton Button with icons is also provided.
To create a button with an icon, first define ImageButton in the layout file, and then set the icon to be displayed using the setImageDrawable method.
Note:
You can directly set the button icon in the layout file, as shown in figure
Android: src = "@ drawable/icon1 ″
You can also set custom icons in the program.
Imgbtn3.setImageDrawable (getResources (). getDrawable (R. drawable. icon2 ));
We can also use the built-in icons.
Imgbtn4.setImageDrawable (getResources (). getDrawable (android. R. drawable. sym_call_incoming ));
After setting the button icon, you need to set the listener setOnClickListener for the button to capture and process the event.
The following example shows the layout composed of four icon buttons. The three buttons are custom and the fourth button is system, when you click button 1, the dialog box is displayed. when you click button 2 and click OK, you can change the icon of button 2 to the icon of button 3. when you click button 3, the icon of button 3 is changed to the system call icon. click Button 4 and a prompt dialog is displayed.
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); // Obtain Four ImageButton objects, respectively. imgbtn1 = (ImageButton) findViewById (R. id. imagebutton1); imgbtn2 = (ImageButton) findViewById (R. id. imagebutton2); imgbtn3 = (ImageButton) findViewById (R. id. imagebutton3); imgbtn4 = (ImageButton) findViewById (R. id. imagebutton4); // Set the icon for ImageButton respectively // imgbtn1 is already in main. the icon is set in the xml layout, so it is not set here (the setting icon can be set in the program or in the layout file) imgbtn2.setImageDrawable (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 // The following sets the event listener imgbtn1.setOnClickListener (new Button. onClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub Dialog dialog = new AlertDialog. builder (ImageButtonTest. this ). setTitle ("prompt "). setMessage ("I'm 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 public void onClick (View v) {// TODO Auto-generated method stub Dialog dialog = new AlertDialog. builder (ImageButtonTest. this ). setTitle ("prompt "). setMessage ("I'm ImageButton2, and I want to use the ImageButton3 icon "). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub imgbtn2.setImageDrawable (getResources (). getDrawable (R. drawable. icon2 ));}}). create (); dialog. show () ;}}); imgbtn3.setOnClickListener (new Button. onClickListener () {@ Override public void onClick (View v) {// TODO Auto-generated method stub Dialog dialog = new AlertDialog. builder (ImageButtonTest. this ). setTitle ("prompt "). setMessage ("I'm ImageButton3, and 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 public void onClick (View v) {// TODO Auto-generated method stub Dialog dialog = new AlertDialog. builder (ImageButtonTest. this ). setTitle ("prompt "). setMessage ("I am using the system icon "). setPositiveButton ("OK", new DialogInterface. onClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stub // corresponding processing operation }}). create (); dialog. show ();}});}}
Layout file main. xml
The running effect is as follows:
The above is the content of the Android UI control series: ImageButton (with the icon). For more information, see The PHP Chinese website (www.php1.cn )!