AlertDialog 使用 扩展

本文详细介绍了如何在Android应用中利用AlertDialog进行弹窗提示,并提供了自定义布局实现及取消操作的代码示例。通过实战演示,帮助开发者掌握AlertDialog的基本用法及其个性化定制。

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

AlertDialog

 

 

[功能]

也是一种Dialog

 

 

[原理]

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

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

 

[代码 步骤]

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

Java代码 复制代码  收藏代码
  1. ab = new AlertDialog.Builder(this);  
ab = new AlertDialog.Builder(this);

 

Java代码 复制代码  收藏代码
  1. ab.setTitle("HelloAlert").setMessage("Warning: its Alert Demo!").setIcon(R.drawable.robot);  
ab.setTitle("HelloAlert").setMessage("Warning: its Alert Demo!").setIcon(R.drawable.robot);

 

 

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

Java代码 复制代码  收藏代码
  1. aDialog = ab.create();  
aDialog = ab.create();

 

 

3. 弹出 AlertDialog

Java代码 复制代码  收藏代码
  1. findViewById(R.id.button).setOnClickListener(new OnClickListener(){   
  2.             public void onClick(View v) {   
  3.                 // TODO Auto-generated method stub   
  4.                 aDialog.show();   
  5.             }   
  6.         });  
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()
不过因为该监听器只负责监听按键事件 而鼠标点击是不管的 所以请点击任意按键关闭之

 

Java代码 复制代码  收藏代码
  1. aDialog.setOnKeyListener(new OnKeyListener(){   
  2.   
  3.             @Override  
  4.             public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {   
  5.                 // TODO Auto-generated method stub   
  6.                 aDialog.dismiss();   
  7.                    
  8.                 return true;   
  9.             }   
  10.                
  11.         });  
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代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:padding="10dp"  
  7.     >  
  8. <ImageView     
  9.     android:id="@+id/image"  
  10.     android:layout_width="wrap_content"    
  11.     android:layout_height="wrap_content"    
  12.     android:src="@drawable/robot" />  
  13. <LinearLayout    
  14.     android:orientation="vertical"  
  15.     android:layout_width="wrap_content"  
  16.     android:layout_height="wrap_content"  
  17.     >  
  18. <TextView     
  19.     android:id="@+id/title"  
  20.     android:layout_width="wrap_content"    
  21.     android:layout_height="wrap_content"    
  22.     android:text="HelloAlert!"  
  23.     />  
  24. <TextView     
  25.     android:id="@+id/message"  
  26.     android:layout_width="wrap_content"    
  27.     android:layout_height="wrap_content"    
  28.     android:paddingTop="10dip"  
  29.     />  
  30.  </LinearLayout>  
  31. </LinearLayout>  
<?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

Java代码 复制代码  收藏代码
  1. view = this.getLayoutInflater().inflate(R.layout.hello, null);  
view = this.getLayoutInflater().inflate(R.layout.hello, null);

 

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

Java代码 复制代码  收藏代码
  1. ab.setView(view);   
  2.   
  3. aDialog = ab.create();  
ab.setView(view);

aDialog = ab.create();

 

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

Java代码 复制代码  收藏代码
  1. TextView title = (TextView) view.findViewById(R.id.title);   
  2.         title.setTextSize(20);   
  3.         title.setTextColor(Color.RED);   
  4.         title.setText("HelloAlert");   
  5.            
  6.         TextView message = (TextView) view.findViewById(R.id.message);   
  7.         message.setText("Warning: it's Alert Demo!");  
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

Java代码 复制代码  收藏代码
  1. findViewById(R.id.button).setOnClickListener(new OnClickListener(){   
  2.             public void onClick(View v) {   
  3.                 // TODO Auto-generated method stub   
  4.                 aDialog.show();   
  5.             }   
  6.         });  
findViewById(R.id.button).setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				// TODO Auto-generated method stub
				aDialog.show();
			}
        });

 

* 取消 AlertDialog

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

 

Java代码 复制代码  收藏代码
  1. view.setOnClickListener(new OnClickListener(){   
  2.             public void onClick(View v) {   
  3.                 // TODO Auto-generated method stub   
  4.                 aDialog.dismiss();   
  5.             }   
  6.         });  
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、付费专栏及课程。

余额充值