ImageButton is an image Button. You can specify an image resource. Other operations are the same as Button. Here you are familiar with the usage of the dialog box. The Code is as follows:
Xml Code
<? 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">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
<ImageButton
Android: id = "@ + id/ib1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
<ImageButton
Android: id = "@ + id/ib2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
<ImageButton
Android: id = "@ + id/ib3"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
<ImageButton
Android: id = "@ + id/ib4"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
</LinearLayout>
Java code
Package zziss. android. imagebuttontest;
Import android. app. Activity;
Import android. app. AlertDialog;
Import android. content. DialogInterface;
Import android. OS. Bundle;
Import android. view. View;
Import android. widget. ImageButton;
Import android. widget. Toast;
Public class ImageButtonTestActivity extends Activity implements android. view. View. OnClickListener
, Android. content. DialogInterface. OnClickListener {
/** Called when the activity is first created .*/
Private ImageButton ib1;
Private ImageButton ib2;
Private ImageButton ib3;
Private ImageButton ib4;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Ib1 = (ImageButton) this. findViewById (R. id. ib1 );
Ib2 = (ImageButton) this. findViewById (R. id. ib2 );
Ib3 = (ImageButton) this. findViewById (R. id. ib3 );
Ib4 = (ImageButton) this. findViewById (R. id. ib4 );
Ib1.setImageResource (R. drawable. phone );
Ib2.setImageResource (R. drawable. tetris );
Ib3.setImageResource (android. R. drawable. btn_star );
Ib4.setImageResource (android. R. drawable. sym_action_call );
Ib1.setOnClickListener (this );
Ib2.setOnClickListener (this );
Ib3.setOnClickListener (this );
Ib4.setOnClickListener (this );
}
@ Override
Public void onClick (View v ){
// TODO Auto-generated method stub
ImageButton ib = (ImageButton) v;
AlertDialog dlg = new AlertDialog. Builder (this)
. SetTitle ("click ")
. SetMessage (Integer. toString (ib. getId ()))
. SetPositiveButton ("OK", this)
. SetNegativeButton ("cancel", this)
. Create ();
Dlg. show ();
}
@ Override
Public void onClick (DialogInterface dialog, int which ){
// TODO Auto-generated method stub
If (which = DialogInterface. BUTTON_POSITIVE)
{
Toast toast = Toast. makeText (this, "dialog box", Toast. LENGTH_LONG );
Toast. show ();
}
Else
{
Toast toast = Toast. makeText (this, "dialog 222", Toast. LENGTH_LONG );
Toast. show ();
}
}
}