首页 > 代码库 > 赵雅智_android_ui_dialog

赵雅智_android_ui_dialog

运行效果:


方法一

public void openDialog(View v) {
		// 打开一个对话框
		// 构建者对象
		AlertDialog.Builder builder = new Builder(this);
		// 构建者创建一个对话框
		AlertDialog alertDialog = builder.create();
		// 设置对话框的标题
		alertDialog.setTitle("我是对话框");
		// 设置对话框内容
		alertDialog.setMessage("你是否真的要退出程序?");
		// 设置对话框的图标
		alertDialog.setIcon(R.drawable.ic_launcher);

		alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定",
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						Toast.makeText(MainActivity.this, "确定",
								Toast.LENGTH_LONG).show();
					}
				});

		alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "中立",
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(MainActivity.this, "中立",
								Toast.LENGTH_LONG).show();
					}
				});

		alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消",
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(MainActivity.this, "取消",
								Toast.LENGTH_LONG).show();
					}
				});

		//必须设置
		alertDialog.show();
	}

方法2

public void openDialog(View v) {
		new AlertDialog.Builder(this).setTitle("我是对话框").setMessage("你是否真的要退出程序")
		.setIcon(R.drawable.ic_launcher).setPositiveButton("确定", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				Toast.makeText(MainActivity.this, "确定",
						Toast.LENGTH_LONG).show();
			}
		}).setNeutralButton("中立", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(MainActivity.this, "中立",
								Toast.LENGTH_LONG).show();
					}
				}).setNegativeButton("取消",
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(MainActivity.this, "取消",
								Toast.LENGTH_LONG).show();
					}
				}).create().show();

	 
	}

方法3

新建一个activity,在AndroidMenifest.xml中设置Theme主题
  android:theme="@android:style/Theme.Dialog"