除了可以使用Android系统自带的Button(按钮)外,在Android平台中,我们还可以制作带图标的按钮,这就需要使用ImageButton组件鸟
要制作带图标的按钮,首先要在布局文件中定义ImageButton,然后通过setImageDrawable方法来设置按钮要显示的图标。同样需要对按钮设置事件监听 setOnClickListener,以此来捕捉事件并处理。 我们先看看这个例子的运行效果。








示例代码如下:
package com.xiaohang;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.app.AlertDialog.Builder;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 Activity01 extends Activity {    /** Called when the activity is first created. */TextView textView;ImageButton imageButton1,imageButton2,imageButton3,imageButton4;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                textView = (TextView)findViewById(R.id.TextView01);        imageButton1 = (ImageButton)findViewById(R.id.ImageButton01);        imageButton2 = (ImageButton)findViewById(R.id.ImageButton02);        imageButton3 = (ImageButton)findViewById(R.id.ImageButton03);        imageButton4 = (ImageButton)findViewById(R.id.ImageButton04);                //给按钮设置使用的图标,由于button1,button2,button3,已经在xml文件中设置了这里就不设置了        imageButton4.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_call_incoming));                //以下分别为每个按钮设置事件监听 setOnClickListener        imageButton1.setOnClickListener(new Button.OnClickListener(){public void onClick(View v) {//对话框   Builder是AlertDialog的静态内部类Dialog dialog = new AlertDialog.Builder(Activity01.this)//设置对话框的标题.setTitle("小航提示")//设置对话框要显示的消息.setMessage("我真的是ImageButton1")//给对话框来个按钮 叫“确定定” ,并且设置监听器 这种写法也真是有些BT.setPositiveButton("确定定", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int which) {//点击 "确定定" 按钮之后要执行的操作就写在这里}}).create();//创建按钮dialog.show();//显示一把}        });                imageButton2.setOnClickListener(new Button.OnClickListener(){public void onClick(View v) {Builder dialog = new AlertDialog.Builder(Activity01.this);dialog.setTitle("提示");dialog.setMessage("我是ImageButton2,我要使用ImageButton3的图标");dialog.setPositiveButton("确定", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int which) {//好了我成功把Button3的图标掠夺过来imageButton2.setImageDrawable(getResources().getDrawable(R.drawable.button3));}}).create();//创建按钮dialog.show();}        });                imageButton3.setOnClickListener(new Button.OnClickListener(){public void onClick(View v) {Builder dialog = new AlertDialog.Builder(Activity01.this);dialog.setTitle("提示");dialog.setMessage("我是ImageButton3,我要使用系统打电话图标");dialog.setPositiveButton("确定", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int which) {//把imageButton3的图标设置为系统的打电话图标imageButton3.setImageDrawable(getResources().getDrawable(android.R.drawable.sym_action_call));}}).create();//创建按钮dialog.show();}        });                imageButton4.setOnClickListener(new Button.OnClickListener(){public void onClick(View v) {Builder dialog = new AlertDialog.Builder(Activity01.this);dialog.setTitle("提示");dialog.setMessage("我没钱买图标使用的是系统图标");dialog.setPositiveButton("确定", new DialogInterface.OnClickListener(){public void onClick(DialogInterface dialog, int which) {}}).create();//创建按钮dialog.show();}        });    }}



布局文件
<?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>
平台Android 2.0 API Level 5
最后再给大家贡献2个 图标 搜索 网站。很给力的。
http://findicons.com/
http://www.iconfinder.com/
图片见附件
源码已上传 见附件。

更多相关文章

  1. Android(安卓)Fragment用法详解(2)--动态添加Fragment
  2. Android(安卓)自定义搜索框(带搜索图标、清除图标、语音图标)
  3. android java开发 第二天 之熟悉 程序开发流程
  4. Android(安卓)Studio Intent使用(显式、隐式) 第一行代码
  5. 新浪微博布局学习――妙用TabHost
  6. Android(安卓)View 仿iOS SwitchButton
  7. Android编程基础之简单Button事件响应综合提示控件Toast应用示例
  8. Android中的ANR异常及traces解析
  9. Android倒计时定时器CountDownTimer的用法

随机推荐

  1. react-native启动流程(android端)
  2. 浅析android的mvp模式
  3. Android零基础入门第84节:引入Fragment原
  4. Android实现自定义字体格式液晶数字
  5. Android中集成支付宝
  6. Android(安卓)系统复习面试系列(二)反射原
  7. 一步一步在 Windows下搭建 React Native
  8. Android(安卓)Studio 解决引入java.awt.*
  9. android string.xml文件中的整型和string
  10. Android的桌面上的应用介绍