欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AlertDialog对话框

程序员文章站 2022-03-15 10:34:16
swich (v.getId()) {// 普通对话框case R.id.btn_dialog_one:alert = null;builder = new AlertDialog.Builder(mContext);alert = builder.setIcon(R.mipmap.ic_icon_fish).setTitle("系统提示:").setMessage("这是一条普通的AlertDialog").setNegativeButton("取消", new Dial...

swich (v.getId()) {

// 普通对话框
case R.id.btn_dialog_one:
	alert = null;
	builder = new AlertDialog.Builder(mContext);
	alert = builder.setIcon(R.mipmap.ic_icon_fish)
		.setTitle("系统提示:")
		.setMessage("这是一条普通的AlertDialog")
		.setNegativeButton("取消", new DialogInterfach.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
							Toask.makeText(mContext, "你点击了取消按钮", Toast.LENGTH_SHORT).show();
						}
			})
		 .setPositiveButton("确定", new DIalogInterface.OnClickListener() {
					    @Override
						public void onClick(DialogInterface dialog, int which) {
							 Toask.makeText(mContext, "你点击了确定按钮",Toast.LENGTH_SHORT).show();
							}
				}).create();
alert.show();
break;

// 普通列表对话框
case R.id.btn_dialog_two:
	final Strng[] lesson = new String[] {"语文", "数学", "英语", "化学"};
	alert = null;
	builder = new AlertDIalog.Builder(mContext);
	alert = builder.setIcon(R.mipmap.ic_icon_fish)
		.setTitle("选择课程")
		.setItems(lesson, new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), "你选择了" + lesson[which], Toast.LENGTH_SHORT).show();
			}
		}).create();
alert.show();
break;

// 单选列表对话框
case R.id.bn_dialog_three:
	final String[] fruits = new String[] {"苹果", "雪梨", "香蕉"};
	alert = null;
	builder = new AlertDialog.Builder(mContext);
	alert = builder.setIcon(R.mipmap.ic_icon_fish)
		.setTitle("选择你喜欢得水果")
		.setSingleChoiceItems(fruits, 0, new DIalogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(getApplicationContext(), "你选择了" + fruits[which], Toast.LENGTH_SHORT).show();
			}
		}).create();
alert.show();
breal;

// 多选列表对话框
case R.id.btn_dialog_four:
	final String[] menu = new String[] {"豆腐", "牛腩", "白切鸡"};
	// 定义一个用来记录整个列表状态的 boolean 数组
	checkItems = new boolean[] {false, false, false};
	alert = null;
	builder = new AlertDialog.Builder(mContext);
	alert = builder.setIcon(R.mipmap.ic_icon_fish)
		.setMultiChoiceItems(menu, checkItems, new DialogInterface.OnMultiChoiceClickListener() {
			@Overrird
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				checkItem[which] = isChecked;
			}
		})
		.setPositiveButton("确定", new DIalogInterface.OnClickListener() {
					    @Override
						public void onClick(DialogInterface dialog, int which) {
							 String result = "";
							 for (int i = 0; i < checkItems.length; I++) {
							 	if(checkItems[i]) {
									result += menu[i] + "";
								}
							 }
							 Toask.makeText(getApplicationContext(), "你点击了" + result,Toast.LENGTH_SHORT).show();
							}
				})
				.setCancelable(false)
				.create();
alert.show();
break;

}

本文地址:https://blog.csdn.net/weixin_41631413/article/details/108849088

相关标签: 笔记 android