android 创建Dialog



   android使用ShowDialog来显示一个Dialog,当第一次调用ShowDialog时,会调用Activity的onCreateDialog方法,我们只需重写该方法,在改方法中创建一个Dialog.
而onPrepareDialog(int, Dialog)在Dialog显示时每次都会被调用。因此如果要动态修改dialog的信息,可以在改方法中实现.
下面给出使用实例:

	showDialog(v.getId()); // 调用showDialog之后会回调onCreateDialog


@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		switch (id) {
			case R.id.btnTwobtn:
				return CreateDialog();
			case R.id.btnthreebtn:
				return CreateDialog3();

			case R.id.btnInput:
				return CreateDialogLogin();

			case R.id.btnwait:
				// 启动线程来关闭等待框
				new Thread() {

					@Override
					public void run() {
						int i = 0;
						while (i < 10) {

							try {
								Thread.sleep(1000);
							}
							catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							i++;
						}
						dismissDialog(R.id.btnwait);
					}
				}.start();
				return CreateProcessDialog();
			case R.id.btnTimePicker:
				return CreateTimePickerDialog();
			default:
				return CreateDialog();
		}

	}

	// 第一个按钮
	private Dialog CreateDialog() {

		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("dialog test");
		builder.setMessage("点击按钮测试一下吧,看看title的变化");
		builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				setTitle("click ok");
			}

		});
		builder.setNegativeButton("cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						setTitle("click cancel");
					}
				});

		return builder.create();
	}

	// 第2个按钮
	private Dialog CreateDialog3() {

		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("dialog test");
		builder.setMessage("点击按钮测试一下吧,看看3个按钮对title的变化");
		builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				setTitle("click ok");
			}

		});
		builder.setNegativeButton("cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						setTitle("click cancel");
					}
				});
		builder.setNeutralButton("详细", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				setTitle("click detail");
			}
		});
		return builder.create();
	}

	// 第3个按钮 弹出输入框
	private Dialog CreateDialogLogin() {

		AlertDialog.Builder builder = new Builder(this);
		// 加载layout文件
		LayoutInflater inflater = LayoutInflater.from(this);
		View view = inflater.inflate(R.layout.logindialog, null);
		builder.setView(view);
		builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				setTitle("click ok");
			}

		});
		builder.setNegativeButton("cancel",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						setTitle("click cancel");
					}
				});
		builder.setNeutralButton("详细", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				setTitle("click detail");
			}
		});
		return builder.create();
	}

	// 第4个按钮 等待框
	private Dialog CreateProcessDialog() {
		ProgressDialog dialog = new ProgressDialog(this);
		dialog.setMessage("正在运行,请等待...");

		return dialog;

	}

	// 第5个按钮 时间选择框
	private Dialog CreateTimePickerDialog() {
		TimePickerDialog dialog = new TimePickerDialog(this, 0,
				mTimeSetListener, 0, 0, false);
		return dialog;

	}

	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {

		@Override
		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
			int hour = hourOfDay;
			int minute1 = minute;
			Toast.makeText(getBaseContext(),
					"You have selected : " + hour + ":" + minute1,
					Toast.LENGTH_SHORT).show();
		}
	};

运行结果如下:
android 创建Dialog_第1张图片
 


android 创建Dialog_第2张图片
 
android 创建Dialog_第3张图片
 

 
android 创建Dialog_第4张图片
 
如果我们想把一个Activity显示为一个Dialog形式,可以在androidManifest.xml中的activity的属性设为android:theme="@android:style/Theme.Dialog,伪装为dialog

           

 

 

你可能感兴趣的:(android)