【Android】对话框AlertDialog的使用(1)

【Android】对话框AlertDialog的使用(一)

对话框的创建和使用有多种方式,在此,只使用最简单的方式,就是直接给按钮增加监听器,然后,弹出所需要的对话框。

一、普通提示对话框

		findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(DialogDemo.this)
						.setIcon(R.drawable.gong1)
						.setTitle("提示文字Dialog")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialog,
											int which) {
										// TODO Auto-generated method stub

									}
								}).setNegativeButton("取消", null).create()
						.show();
			}
		});

效果图

【Android】对话框AlertDialog的使用(1)



二、长文字对话框

		findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(DialogDemo.this)
						.setIcon(R.drawable.gong2)
						.setTitle("提示信息Dialog")
						.setMessage(
								"发现新版本(1.0.1.260)"
										+ "\n"
										+ "【全新界面】 全新多屏首页,界面更友好、美观、流程\n"
										+ "【无线文件传输】 点击无线传输按钮,访问提示地址,可在同一无线网内实现手机和电脑文件互传\n"
										+ "【快捷面板】 快捷面板全新改造,整合多项功能按钮,图标更大更直观,强大易用\n"
										+ "    是否升级最新版本?")
						.setPositiveButton("确定",
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int whichButton) {

										/* User clicked OK so do some stuff */
									}
								}).setNeutralButton("中间按钮", null)
						.setNegativeButton("取消", null).create().show();
			}
		});

效果图
【Android】对话框AlertDialog的使用(1)

三、单选项选择对话框

		findViewById(R.id.button3).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(DialogDemo.this)
						.setTitle("单选Dialog")
						.setItems(R.array.dialog_arrays,
								new DialogInterface.OnClickListener() {
									public void onClick(DialogInterface dialog,
											int which) {
										String[] items = getResources()
												.getStringArray(
														R.array.dialog_arrays);
										Toast.makeText(
												DialogDemo.this,
												"You selected: " + which
														+ " , " + items[which],
												1000).show();
									}
								}).create().show();
			}
		});

其中,需要在res/values文件夹下新建arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="dialog_arrays">
        <item >浙江</item>
        <item >山西</item>
        <item >山东</item>
        <item >河南</item>
        <item >河北</item>
        <item >广东</item>
    </string-array>
    
</resources>

效果图
【Android】对话框AlertDialog的使用(1)

四、进度条对话框

先定义全局变量:
	private ProgressDialog pDialog;
	private Handler handler;
	private int mProgress;

		findViewById(R.id.button4).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				pDialog = new ProgressDialog(DialogDemo.this);
				pDialog.setIcon(R.drawable.gong3);
				pDialog.setTitle("进度条Dialog");
				pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				pDialog.setMax(100);
				pDialog.setButton(DialogInterface.BUTTON_POSITIVE, "左键",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
							}
						});
				pDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "右键",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						});
				pDialog.setProgress(0);
				pDialog.show();
				mProgress = 0;
				// 执行完毕后给handler发送一个空消息
				handler.sendEmptyMessage(0);
			}
		});
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (mProgress >= 100) {
					pDialog.dismiss();
				} else {
					mProgress++;
					// ProgressBar进度值加1
					pDialog.incrementProgressBy(1);
					// 延迟100毫秒后发送空消息
					handler.sendEmptyMessageDelayed(0, 100);
				}
			}
		};

效果图
【Android】对话框AlertDialog的使用(1)

五、单独的进度条

只需要将上面的对话框中,一些无用的按钮和标题去掉即可。
		findViewById(R.id.button5).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				pDialog = new ProgressDialog(DialogDemo.this);
				pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				pDialog.setMax(100);
				pDialog.setProgress(0);
				pDialog.show();
				mProgress = 0;
				handler.sendEmptyMessage(0);
			}
		});
		handler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (mProgress >= 100) {
					pDialog.dismiss();
				} else {
					mProgress++;
					// ProgressBar进度值加1
					pDialog.incrementProgressBy(1);
					// 延迟100毫秒后发送空消息
					handler.sendEmptyMessageDelayed(0, 100);
				}
			}
		};

效果图
【Android】对话框AlertDialog的使用(1)
2楼jason0539昨天 22:50
不错。
Re: oYunTaoLianWu昨天 22:57
回复jason0539n多谢~~
1楼u011960402昨天 09:15
写的很好,顶一个
Re: oYunTaoLianWu昨天 12:14
回复u011960402n互相学习,多谢~