1.多選對話框

54d66643-750f-3661-89de-546ad3482b51  

 

void showMeultiChoicesDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setTitle("Computer Language");

		final String[] items = { "c", "c++", "c#", "java" };

		final boolean[] choices = { false, false, false, false };

		builder.setMultiChoiceItems(items, choices,
				new DialogInterface.OnMultiChoiceClickListener() {

					String temp = "";

					@Override
					public void onClick(DialogInterface dialog, int which,
							boolean isChecked) {

						if (isChecked) {

							temp += items[which] + " ";

							Toast.makeText(MainActivity.this, temp,
									Toast.LENGTH_LONG).show();

						}

					}

				});

		AlertDialog ad = builder.create();

		ad.show();

	}

 

 
 2.列表對話框

 


e00b8fed-a4eb-3f05-b597-9d105c726a39  
  void showListDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setTitle("Computer Language");

		final String[] items = { "c", "c++", "c#", "java" };

		builder.setItems(items, new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				Toast.makeText(MainActivity.this, items[which],
						Toast.LENGTH_LONG).show();

			}
		});

		AlertDialog ad = builder.create();

		ad.show();

	}

 

 3. 單選按鈕對話框


cafef009-762a-3285-9068-084468039503  
 void showSingleChoicesDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setTitle("Computer Language");

		final String[] items = { "c", "c++", "c#", "java" };

		builder.setSingleChoiceItems(items, -1,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

						Toast.makeText(MainActivity.this, items[which],
								Toast.LENGTH_LONG).show();

						dialog.cancel();

					}

				});

		AlertDialog ad = builder.create();

		ad.show();

	}

 4. 根據XML 布局文件自定義對話框

 


15d57be3-28ce-3828-8feb-e08fe46c1530  
 // 對話框的內容是其它自定義View

	void customDialog() {

		AlertDialog.Builder builder = new AlertDialog.Builder(this);

		builder.setTitle("登陆窗口");

		LayoutInflater inflater = LayoutInflater.from(this);

		View v = inflater.inflate(R.layout.login, null);
		// 例如,使用AlertDialog來實現登陆窗口
		builder.setView(v);

		builder.setPositiveButton("登陆", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

			}

		});

		builder.setNegativeButton("注冊", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {

				dialog.cancel();

			}

		});

		AlertDialog ad = builder.create();

		ad.show();

	}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()