AlertDialog窗口

一、AlertDialog窗口的新建和显示

 

new AlertDialog.Builder(EX03_12.this)
						.setTitle(R.string.app_about)
						.setMessage(R.string.app_about_msg)
						.setPositiveButton(R.string.str_ok,
								new DialogInterface.OnClickListener() {
									public void onClick(
											DialogInterface dialoginterface,
											int i) {
										dialoginterface.cancel();
									}
								}).show();

 

二、新建具有菜单功能的AlertDialog窗口

 

new AlertDialog.Builder(MultiAlertDialog.this)
		.setTitle(R.string.app_name)
		.setItems(R.array.items,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,
							int whichcountry) {
						String[] aryShop = getResources()
								.getStringArray(
										R.array.items);
						new AlertDialog.Builder(MultiAlertDialog.this)
								.setMessage(aryShop[whichcountry])
								.setNeutralButton(
										"确定",
										new DialogInterface.OnClickListener() {
											public void onClick(DialogInterface dialog,int whichButton) { 
												/* 在这里处理要作的事 */
											}
										}).show();
					}
				})
		.setNegativeButton("取消",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface d, int which) {
						d.dismiss();
					}
				}).show();


AlertDialog窗口


AlertDialog窗口

 

二、返回键AlertDialog窗口提示退出

 

public boolean onKeyDown(int keyCode, KeyEvent event) {

		if(keyCode == KeyEvent.KEYCODE_BACK){

			new AlertDialog.Builder(this)

				.setIcon(android.R.drawable.ic_menu_help)

				.setTitle("提示")

				.setMessage("您确定要退出" + getResources().getString(R.string.app_name) + "?")

				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

					}

				})

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

					public void onClick(DialogInterface dialog, int whichButton) {

						android.os.Process.killProcess(android.os.Process.myPid());     //获取PID

					}

				}).show();		

			return true;

		}else{		

			return super.onKeyDown(keyCode, event);

		}

	}
 


AlertDialog窗口

 

 

你可能感兴趣的:(AlertDialog)