Android中几种常用的对话框

效果图

简单对话框

按钮的点击事件中调用showListDialog方法,代码如下
private void showListDialog() {
		// TODO Auto-generated method stub
		new AlertDialog.Builder(this).setTitle("选择省份")
				.setItems(pro, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						final AlertDialog ad = new AlertDialog.Builder(
								MainActivity.this).setMessage(
								"你已经选择了:" + which + ":" + pro[which]).show();
						android.os.Handler handler = new android.os.Handler();
						handler.postDelayed(new Runnable() {

							@Override
							public void run() {
								// TODO Auto-generated method stub
								ad.dismiss();
							}
						}, 5 * 1000);
					}
				}).show();
	}

单选对话框

调用showSingleDialog方法
private void showSingleDialog() {
		// TODO Auto-generated method stub
		new AlertDialog.Builder(this).setTitle("选择省份")
				.setSingleChoiceItems(pro, 1, ButtonOnClick)
				.setPositiveButton("确定", ButtonOnClick)
				.setNegativeButton("取消", ButtonOnClick).show();
	}
ButtonOnClick监听器定义为:
private class ButtonOnClick implements DialogInterface.OnClickListener {
		//用于获得列表中被点击选项的下标值
		private int index;

		public ButtonOnClick(int index) {
			this.index = index;

		}

		@Override
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			if (which >= 0) {
				index = which;
			} else {
				if (which == DialogInterface.BUTTON_POSITIVE) {
					new AlertDialog.Builder(MainActivity.this).setMessage(
							"你选择的是:" + pro[index]).show();
				}
				if (which == DialogInterface.BUTTON_NEGATIVE) {
					new AlertDialog.Builder(MainActivity.this).setMessage(
							"你未选择任何省份").show();
				}
			}
		}

	}
}

多选对话框

private void showMultiDialog() {
		// TODO Auto-generated method stub
		AlertDialog ad = new AlertDialog.Builder(this)
				.setTitle("选择省份")
				.setMultiChoiceItems(pro,
						new boolean[] { false, false, false },
						new DialogInterface.OnMultiChoiceClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which, boolean isChecked) {
								// TODO Auto-generated method stub

							}
						})
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						int count = listView.getCount();
						String s = "你选择了 ";
						for (int i = 0; i < pro.length; i++) {
							if (listView.getCheckedItemPositions().get(i)) {
								s += listView.getAdapter().getItem(i);
							}
						}
						if (listView.getCheckedItemPositions().size() > 0) {
							new AlertDialog.Builder(MainActivity.this)
									.setMessage(s).show();
						} else {
							new AlertDialog.Builder(MainActivity.this)
									.setMessage("您未选择任何省份").show();
						}
					}
				}).setNegativeButton("取消", null).create();
		listView = ad.getListView();
		ad.show();
	}

进度对话框


//style用于判断显示哪种进度对话框
	private void showProcessDialog(int style) {
		// TODO Auto-generated method stub
		progressDialog = new ProgressDialog(this);
		progressDialog.setTitle("正在下载...");
		progressDialog.setMessage("请稍后...");
		progressDialog.setProgressStyle(style);
		progressDialog.setMax(MAX_PROCESS);
		progressDialog.setButton("暂停", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				processHandler.removeMessages(1);
			}
		});

		progressDialog.setButton2("取消", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				processHandler.removeMessages(1);
				process = 0;
				progressDialog.setProgress(0);
			}
		});
		progressDialog.show();
		processHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (process >= MAX_PROCESS) {
					process = 0;
					progressDialog.dismiss();
				} else {

					process++;
					progressDialog.incrementProgressBy(1);
					processHandler.sendEmptyMessageDelayed(1,
							50 + new Random().nextInt(500));
				}
			}
		};

		process = (process > 0) ? process : 0;
		progressDialog.setProgress(process);
		processHandler.sendEmptyMessage(1);
	}

自定义对话框

private void showDiyDialog() {
		// TODO Auto-generated method stub
		LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.login, null);
		new AlertDialog.Builder(this).setTitle("用户登录").setView(linearLayout)
				.setPositiveButton("登陆", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Toast toast = Toast.makeText(getApplicationContext(),
								"登陆成功", Toast.LENGTH_SHORT);
						toast.show();
					}
				}).setNegativeButton("取消", null).show();
	}
布局文件login.xml为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="用户名" />

        <EditText
            android:id="@+id/user_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >

        </EditText>
        
    </LinearLayout>
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="密码" />

        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword" >

            <requestFocus />
        </EditText>
        
    </LinearLayout>

</LinearLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值