常见对话框

1.确定取消对话框:

public void click1(View v) {
	// 对话框的创建器
	AlertDialog.Builder builder = new Builder(this);
	builder.setTitle("我是对话框");
	builder.setMessage("我是对话框中的内容!");
	builder.setPositiveButton("确定", new OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			Toast.makeText(getApplicationContext(), "点击了确定!", 0).show();
		}
	});
	builder.setNegativeButton("取消", new OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			// 什么都不写,默认实现就是关闭掉对话框
		}
	});
	builder.create().show();
}


2.单选对话框:


public void click2(View v) {
	AlertDialog.Builder builder = new Builder(this);
	builder.setTitle("请选择你的性别:");
	final String[] items = { "男", "女" };
	builder.setSingleChoiceItems(items, 0, new OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			Toast.makeText(getApplicationContext(),
					"你的性别是:" + items[which], 0).show();
			dialog.dismiss();
		}
	});
	builder.show();
}


3.多选对话框:


public void click3(View v) {
	AlertDialog.Builder builer = new Builder(this);
	builer.setTitle("请选择你喜欢的球星:");
	final String[] items = { "C罗", "梅西", "伊布", "范佩西" };
	final boolean[] checkedItems = { true, false, true, false };
	builer.setMultiChoiceItems(items, checkedItems,
			new OnMultiChoiceClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which,
						boolean isChecked) {
					checkedItems[which] = isChecked;
					Toast.makeText(getApplicationContext(),
							"你喜欢的球星是:" + items[which], 0).show();
				}
			});
	builer.setPositiveButton("确定", new OnClickListener() {
		StringBuffer sb = new StringBuffer();

		@Override
		public void onClick(DialogInterface dialog, int which) {
			for (int i = 0; i < items.length; i++) {
				if (checkedItems[i]) {
					sb.append(items[i] + ",");
				}
			}
			Toast.makeText(getApplicationContext(),
					"你喜欢的球星是:" + sb.toString(), 0).show();
		}
	});
	builer.show();
}

4.进度条对话框

常见对话框_第1张图片

public void click4(View v){
	ProgressDialog pd = new ProgressDialog(this);
	pd.setTitle("我是进度条对话框");
	pd.setMessage("正在加载。。。请稍等");
	pd.show();
}

5.带进度的进度条对话框

常见对话框_第2张图片

public void click5(View v) {
	final ProgressDialog pd = new ProgressDialog(this);
	pd.setTitle("我是进度条对话框");
	pd.setMessage("正在加载。。。请稍等");
	pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	pd.setMax(100);
	pd.show();
	new Thread(new Runnable() {

		@Override
		public void run() {
			for (int i = 1; i <= 100; i++) {
				SystemClock.sleep(40);
				pd.setProgress(i);
			}
			pd.dismiss();
		}
	}).start();
}

弹出对话框,让用户必须选择的情况下,可以设置返回键无效

builer.setCancelable(false);

默认是true。

你可能感兴趣的:(常见对话框)