android ImageButton示例

本文提供了一个Android应用示例,展示了如何通过按钮触发自定义对话框,并设置对话框标题、消息和按钮操作。代码涵盖了按钮事件监听、图标设置及不同场景下的对话框实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[url]http://byandby.iteye.com/blog/815212[/url]

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值