AlertDialog简析

本文详细介绍了AlertDialog的使用方法,包括创建对话框、设置标题、图标、内容等基本操作,以及如何添加按钮并设置点击事件。此外还展示了如何通过AlertDialog实现列表选择、单选框和复选框等功能。

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



AlertDialog.builer创建对话框。

常用方法:


setTitle  设置标题
setIcon   设置图标
setMessage  设置内容
setItems  设置对话框中显示的项目列表
setView   设置自定义样式

setSingleChoiceItems 设置对话框显示一个单选框
setMultiChoiceItems 设置对话框显示一系列的复选框

setPositiveButton 对话框添加确定按钮
setNegativeButton 对话框添加取消按钮
setNeutralButton 普通按钮
create   创建对话框
show   显示对话框



简单的AlertDialog对话框

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Dialog alertDialog = new AlertDialog.Builder(this)     //创建对话框
   			.setTitle("简单对话框")    //设置标题
   			.setMessage("hello")    //设置内容
   			.setIcon(R.drawable.ic_launcher)  //设置图标
   			.create();     	//完成对话框的创建
		alertDialog.show();     //显示对话框
 	}
}


效果图:





带按钮的AlertDialog对话框

 在上面加几个Button,实现删除操作的提示对话框

     setPositiveButton
     setNegativeButton
     setNeutralButton

 

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Dialog alertDialog = new AlertDialog.Builder(this)
			.setTitle("确定删除?")
			.setMessage("您确定删除这条消息吗?")
			.setIcon(R.drawable.ic_launcher)
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {	//添加确定按钮
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {	//添加取消按钮
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
				}
			})
			.setNeutralButton("查看详情", new DialogInterface.OnClickListener() {	//添加普通按钮
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
				}
			})
			.create();
		alertDialog.show();
	}
}


效果图:





类似ListView的AlertDialog对话框

//核心方法 setItems(CharSequence[] items, final OnClickListener listener)

//参数1,要显示的数据的数组

//参数2,点击某个item的触发事件

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		final String[] arrayWarringStates = new String[] { "齐", "楚", "秦", "燕", "赵", "魏", "韩" };

		Dialog alertDialog = new AlertDialog.Builder(this)
			.setTitle("你喜欢哪个国家?")
			.setIcon(R.drawable.ic_launcher)
			.setItems(arrayWarringStates, new DialogInterface.OnClickListener() { //设置对话框中显示的项目列表
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MainActivity.this,
						"您加入了"+arrayWarringStates[which]+"国", Toast.LENGTH_SHORT).show();
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
				}
			}).create();
		alertDialog.show();
	}
}

效果图:

 



类似RadioButton的AlertDialog

//核心方法 setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)

//参数1,要显示的数据的数组

//参数2,初始值(初始被选中的item),

//参数3,点击某个item的触发事件

public class MainActivity extends Activity {

	private int selectedCountryIndex = 0;		//默认选择项

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final String[] arrayWarringStates = new String[] { "齐", "楚", "秦", "燕","赵", "魏", "韩" };

		Dialog alertDialog = new AlertDialog.Builder(this)
			.setTitle("你喜欢哪个国家?")
			.setIcon(R.drawable.ic_launcher)
			//设置对话框显示一个单选框
			.setSingleChoiceItems(arrayWarringStates, selectedCountryIndex,new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog,int which) {
					selectedCountryIndex = which;
				}
			})
			.setPositiveButton("确认", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Toast.makeText(MainActivity.this,
				"您加入了"+arrayWarringStates[selectedCountryIndex]+"国",Toast.LENGTH_SHORT).show();
					// TODO Auto-generated method stub
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
				}
			}).create();
		alertDialog.show();
	}
}

效果图:

 




类似CheckBox的AlertDialog对话框

//核心方法setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)

//参数1,要显示的数据的数组,

//参数2,选中状态的数组,

//参数3,点击某个item的触发事件

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final String[] arrayWarringStates = new String[] { "齐", "楚", "秦", "燕","赵", "魏", "韩" };
		//选项初始化
        	final boolean[] selectedCountryIndex = new boolean[] {true, true, false, false, false, false, false}; 	
 
       		Dialog alertDialog = new AlertDialog.Builder(this)
		.setTitle("你喜欢哪个国家?")
		.setIcon(R.drawable.ic_launcher)
		//设置对话框显示一系列的复选框 
		.setMultiChoiceItems(arrayWarringStates,selectedCountryIndex,new  DialogInterface.OnMultiChoiceClickListener() {                   
                    @Override 
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
                    	selectedCountryIndex[which] = isChecked; 
                    } 
                })
		.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        StringBuilder stringBuilder = new StringBuilder(); 		//判断复选结果
                        for (int i = 0; i < selectedCountryIndex.length; i++) { 
                            if (selectedCountryIndex[i] == true) 
                            { 								//字符串,用”、”把选项连接起来
                                stringBuilder.append(arrayWarringStates[i] + "、");
                            } 
                        } 
                        Toast.makeText(MainActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show(); 
                    } 
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() { 
 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        // TODO Auto-generated method stub  
                    } 
                })
                .create(); 
        alertDialog.show(); 
    } 
}


效果图:


 




自定义View的AlertDialog

//核心方法 setView

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// 取得自定义View
		// 动态加载布局生成View对象
		LayoutInflater layoutInflater = LayoutInflater.from(this);            
		View myloginView = layoutInflater.inflate(R.layout.activity_main, null);

		Dialog alertDialog = new AlertDialog.Builder(this)
			.setTitle("用户登录")
			.setIcon(R.drawable.ic_launcher)
			.setView(myloginView)
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
				}
			})
			.setPositiveButton("登陆", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
				}
			})
			.create();
		alertDialog.show();
	}
}

效果图:

 

 



另外也可以使用内部类来封装各按钮的点击事件



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值