AlertDialog 使用 扩展

本文详细介绍了AlertDialog的使用方法,包括初始化、展示和取消对话框的过程。此外还提供了自定义布局及添加按钮的步骤。

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

AlertDialog

 

 

[功能]

也是一种Dialog

 

 

[原理]

1. AlertDialog 本身并没有构造函数 即 不可以通过 new AlertDialog(...) 来初始化 而只能通过 AlertDialog.Builder

2. 而 AlertDialog.Builder 比较像是AlertDialog的构造器 用于接收各种和 AlertDialog 有关的参数 然后通过 create() 来创建目标 AlertDialog

 

[代码 步骤]

1. 定义 AlertDialog.Builder 实例 并接受一些参数 如:图片 标题 正文

ab = new AlertDialog.Builder(this);

 

ab.setTitle("HelloAlert").setMessage("Warning: its Alert Demo!").setIcon(R.drawable.robot);

 

 

 2. 根据AlertDialog.Builder 创建 相应的 AlertDialog

aDialog = ab.create();

 

 

3. 弹出 AlertDialog

findViewById(R.id.button).setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				aDialog.show();
			}
        });

 

 

4. 取消 AlertDialog

写道
因为该AlertDialog 所采用的布局是系统的 其只接受Text 不接受Button 但是发现可以在AlertDialog 上面注册按键监听 即AlertDialog.setOnKeyListener()
不过因为该监听器只负责监听按键事件 而鼠标点击是不管的 所以请点击任意按键关闭之

 

aDialog.setOnKeyListener(new OnKeyListener(){

			@Override
			public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
				// TODO Auto-generated method stub
				aDialog.dismiss();
				
				return true;
			}
        	
        });

 

 

 * emulator 运行截图:

 

 

 

 

5. 以上所采用的都是AlertDialog 系统默认的布局 现在说自定义布局的情况 并添加一个用于取消AlertDialog 的 Button

 

* 定义其布局 hello.main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    >
<ImageView  
	android:id="@+id/image"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/robot" />
<LinearLayout 
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
<TextView  
	android:id="@+id/title"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="HelloAlert!"
    />
<TextView  
	android:id="@+id/message"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="10dip"
    />
 </LinearLayout>
</LinearLayout>

 

 

* 通过LayoutInflater 得到上面 hello.xml 布局的 View view

view = this.getLayoutInflater().inflate(R.layout.hello, null);

 

* 指定AlertDialog.Builder 所需的布局 并返回目标AlertDialog

ab.setView(view);

aDialog = ab.create();

 

* 通过 view.findViewById() 得到 目标View 然后设置其内容 如:

TextView title = (TextView) view.findViewById(R.id.title);
        title.setTextSize(20);
        title.setTextColor(Color.RED);
        title.setText("HelloAlert");
        
        TextView message = (TextView) view.findViewById(R.id.message);
        message.setText("Warning: it's Alert Demo!");

 

 

* 弹出 AlertDialog

findViewById(R.id.button).setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				aDialog.show();
			}
        });

 

* 取消 AlertDialog

写道
在整个View 上注册按键监听器 关闭AlertDialog

 

view.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				aDialog.dismiss();
			}
        });

 

 * emulator 运行截图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值