ImageButton exercise (android)

Source: Internet
Author: User

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 ();
}
}




}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.