android ImageButton示例

来源:互联网 发布:windows7共享网络密码 编辑:程序博客网 时间:2024/04/28 19:47
 除了可以使用Android系统自带的Button(按钮)外,在Android平台中,我们还可以制作带图标的按钮,这就需要使用ImageButton组件鸟 
  要制作带图标的按钮,首先要在布局文件中定义ImageButton,然后通过setImageDrawable方法来设置按钮要显示的图标。同样需要对按钮设置事件监听 setOnClickListener,以此来捕捉事件并处理。 我们先看看这个例子的运行效果。 


 

 

 

示例代码如下: 
Java代码  收藏代码
  1. package com.xiaohang;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.Dialog;  
  6. import android.app.AlertDialog.Builder;  
  7. import android.content.DialogInterface;  
  8. import android.os.Bundle;  
  9. import android.view.View;  
  10. import android.widget.Button;  
  11. import android.widget.ImageButton;  
  12. import android.widget.TextView;  
  13.   
  14. public class Activity01 extends Activity {  
  15.     /** Called when the activity is first created. */  
  16.     TextView textView;  
  17.     ImageButton imageButton1,imageButton2,imageButton3,imageButton4;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.           
  23.         textView = (TextView)findViewById(R.id.TextView01);  
  24.         imageButton1 = (ImageButton)findViewById(R.id.ImageButton01);  
  25.         imageButton2 = (ImageButton)findViewById(R.id.ImageButton02);  
  26.         imageButton3 = (ImageButton)findViewById(R.id.ImageButton03);  
  27.         imageButton4 = (ImageButton)findViewById(R.id.ImageButton04);  
  28.           
  29.         //给按钮设置使用的图标,由于button1,button2,button3,已经在xml文件中设置了这里就不设置了  
  30.         imageButton4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));  
  31.           
  32.         //以下分别为每个按钮设置事件监听 setOnClickListener  
  33.         imageButton1.setOnClickListener(new Button.OnClickListener(){  
  34.             public void onClick(View v) {  
  35.                 //对话框   Builder是AlertDialog的静态内部类  
  36.                 Dialog dialog = new AlertDialog.Builder(Activity01.this)  
  37.                 //设置对话框的标题  
  38.                     .setTitle("小航提示")  
  39.                 //设置对话框要显示的消息  
  40.                     .setMessage("我真的是ImageButton1")  
  41.                 //给对话框来个按钮 叫“确定定” ,并且设置监听器 这种写法也真是有些BT  
  42.                     .setPositiveButton("确定定"new DialogInterface.OnClickListener(){  
  43.   
  44.                         public void onClick(DialogInterface dialog, int which) {  
  45.                             //点击 "确定定" 按钮之后要执行的操作就写在这里  
  46.                         }  
  47.                     }).create();//创建按钮  
  48.                     dialog.show();//显示一把  
  49.             }  
  50.         });  
  51.           
  52.         imageButton2.setOnClickListener(new Button.OnClickListener(){  
  53.             public void onClick(View v) {  
  54.                 Builder dialog = new AlertDialog.Builder(Activity01.this);  
  55.                 dialog.setTitle("提示");  
  56.                 dialog.setMessage("我是ImageButton2,我要使用ImageButton3的图标");  
  57.                 dialog.setPositiveButton("确定"new DialogInterface.OnClickListener(){  
  58.                     public void onClick(DialogInterface dialog, int which) {  
  59.                         //好了我成功把Button3的图标掠夺过来  
  60.                         imageButton2.setImageDrawable(getResources().getDrawable(R.drawable.button3));  
  61.                     }  
  62.                 }).create();//创建按钮  
  63.                 dialog.show();  
  64.             }  
  65.         });  
  66.           
  67.         imageButton3.setOnClickListener(new Button.OnClickListener(){  
  68.             public void onClick(View v) {  
  69.                 Builder dialog = new AlertDialog.Builder(Activity01.this);  
  70.                 dialog.setTitle("提示");  
  71.                 dialog.setMessage("我是ImageButton3,我要使用系统打电话图标");  
  72.                 dialog.setPositiveButton("确定"new DialogInterface.OnClickListener(){  
  73.                     public void onClick(DialogInterface dialog, int which) {  
  74.                         //把imageButton3的图标设置为系统的打电话图标  
  75.                         imageButton3.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_action_call));  
  76.                     }  
  77.                 }).create();//创建按钮  
  78.                 dialog.show();  
  79.             }  
  80.         });  
  81.           
  82.         imageButton4.setOnClickListener(new Button.OnClickListener(){  
  83.             public void onClick(View v) {  
  84.                 Builder dialog = new AlertDialog.Builder(Activity01.this);  
  85.                 dialog.setTitle("提示");  
  86.                 dialog.setMessage("我没钱买图标使用的是系统图标");  
  87.                 dialog.setPositiveButton("确定"new DialogInterface.OnClickListener(){  
  88.                     public void onClick(DialogInterface dialog, int which) {  
  89.                           
  90.                     }  
  91.                 }).create();//创建按钮  
  92.                 dialog.show();  
  93.             }  
  94.         });  
  95.     }  
  96. }  



布局文件 
<?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/TextView01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 

<ImageButton 
android:id="@+id/ImageButton01" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/button1" 
/> 

<ImageButton 
android:id="@+id/ImageButton02" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/button2" 
/> 

<ImageButton 
android:id="@+id/ImageButton03" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/button3" 
/> 

<ImageButton 
android:id="@+id/ImageButton04" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"/> 

</LinearLayout>