`
byandby
  • 浏览: 1688673 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android ImageButton示例

阅读更多
  除了可以使用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/
图片见附件
源码已上传 见附件。
  • 大小: 28.4 KB
  • 大小: 34.2 KB
  • 大小: 35.2 KB
分享到:
评论
3 楼 wk814007042 2015-06-29  
非常感谢
2 楼 winchun323 2012-07-17  
感谢楼主!!!
1 楼 guolong2008 2012-07-05  
学习了。楼主不错

相关推荐

    Android 单击选中的ImageButton[]图像数组用法示例.rar

    Android 单击选中的ImageButton[]图像数组用法示例,类似于Radio的功能,不过用图片表现,这样可使Android UI更加友好,视觉更漂亮一些,用户轻触图片,即可选中该数据项,比Radio用户体验更好,如截图所示的选中...

    Android基本控件使用示例

    Android基本控件使用示例,包含以下控件实例: AutoCompleteTextView CheckBox TimePicker DatePicker EditText GridView ImageButton ImageShow ImageView ProgressBar RadioGroup RatingBar SeekBar Spinner Tab

    Android的ImageButton当显示Drawable图片时就不显示文字

    很多人对 Android提供的ImageButton有个疑问,当显示Drawable图片时就不会再显示文字了,其实解决的方法有三种: 第一种:就是图片中就写入文字,但是这样解决会增加程序体积,同时硬编码方式会影响多国语言的发布。...

    android自定义按钮示例(重写imagebutton控件实现图片按钮)

    由于项目这种类型的图片按钮比较多,所以重写了ImageButton类。 代码如下:package me.henji.widget; import android.content.Context;import android.graphics.ColorMatrix;import android.graphics....

    Android开发案例驱动教程 配套代码

    5.1.2 ImageButton 63 5.1.3 ToggleButton 64 5.2 TextView 64 5.3 EditText 65 5.4 RadioButton和RadioGroup 66 5.4.1 RadioButton 66 5.4.2 RadioGroup 67 5.5 CheckBox 68 5.6 ImageView 70 5.7 ...

    疯狂Android讲义源码

     2.3.2 按钮(Button)与图片按钮(ImageButton)组件的功能和  用法 66  2.3.3 使用9Patch图片作为按钮背景 68  2.3.4 单选按钮(RadioButton)和复选  框(CheckBox)介绍与应用 69  2.3.5 状态开关按钮...

    Android应用开发揭秘pdf高清版

    最重要的是还全面介绍了如何利用原生的C,C++(NDK)和Python、Lua等脚本语言(AndroidScriptingEnvironment)来开发Android应用,《Android应用开发揭秘》实战性强,书中的每个知识点都有配精心设计的示例,尤为...

    Android编程入门很简单.(清华出版.王勇).part1

    5.2.7 使用图片按钮——ImageButton 5.2.8 使用复选框——CheckBox 5.2.9实例——请同意本协议 5.2.10 使用单选框——RadioGroup 5.2.11 实例——请选择性别 5.2.12使用下拉列表框——Spinner 5.2.13实例——请选择...

    演示Android通用控件包括TextView, EditText,AutoCompleteTextView等控件

    4、如果一切正常,那么你会在虚拟设备看到常用的控件(包括TextView, EditText,AutoCompleteTextView,MultiAutoCompleteTextView,Button,ImageButton,ToggleButton,CheckBox和RadioButton),根据中文提示操作。...

    Android编程入门很简单.(清华出版.王勇).part2

    5.2.7 使用图片按钮——ImageButton 5.2.8 使用复选框——CheckBox 5.2.9实例——请同意本协议 5.2.10 使用单选框——RadioGroup 5.2.11 实例——请选择性别 5.2.12使用下拉列表框——Spinner 5.2.13实例——请选择...

    Android中设置组件半透明和透明的效果示例

    1、Button或者ImageButton的背景设为透明或者半透明 半透明  &lt;Button android:background=#e0000000 ... /&gt; 透明  &lt;Button android:background=#00000000 ... /&gt; 颜色和不透明度 (alpha) 值以...

    Android中src和background的区别详解

    bg可设置透明度,比如在ImageButton中就可以用android:scaleType控制图片的缩放方式,示例代码如下: &lt;ImageView android:id=@+id/img android:src=@drawable/logo android:scaleType=centerInsid

    Android典型技术模块开发详解

    7.2 ImageButton(图片按钮) 7.3 ToggleButton(开关按钮) 7.4 TextView(文本视图) 7.5 ImageView(图片视图) 7.6 EditText(编辑框) 7.7 RadioButton(单选按钮) 7.8 CheckBox(多选框) 7.9 Spinner(下拉...

    android-gif-drawable:用于在 Android 上显示动画 GIF 的视图和可绘制对象-开源

    通过 JNI 捆绑的 GIFLib 用于渲染帧。 这种方式应该比 WebView 或 ... 如果给定的 drawable 不是 GIF,那么提到的 Views 就像普通的 ImageView 和 ImageButton。 GifTextView 允许您将 GIF 用作复合可绘制对象和背景。

    android 自定义控件 自定义属性详细介绍

    自定义控件在android中无处不见,自定义控件给了我们很大的方便。比如说,一个视图为imageview ,imagebutton ,textview 等诸多控件的组合,用的地方有很多,我们不可能每次都来写3个的组合,既浪费时间,效率又低。...

    Android 中ListView的Item点击事件失效的快速解决方法

    在平常的开发过程中,我们的ListView可能不只是简单的显示下文本或者按钮,更多的是显示复杂的布局,这样的话,我们就得自己写布局和自定义adapter了,一般是继承于BaseAdapter,示例代码见下方。写ListView的点击...

Global site tag (gtag.js) - Google Analytics