Android AlertDialog對話框 學習筆記

特點:

1、當前界面彈出的一個小窗口,用於提示用戶重要的信息,並獲得焦點。

2、用戶要與它進行互動,可以接受用戶輸入的信息,也可以反饋信息給用戶,如下載進度,退出提示等。


常用的對話框:

1、簡單對話框

2、簡單列表對話框

3、單選按鈕對話框

4、多選按鈕對話框

5、自定義列表對話框

6、自定義佈局對話框



對話框包含四個區域:

1、圖標區

2、標題區

3、內容區

4、按鈕區


如何創建對話框:

1、創建AlertDialog.Builder的對象

2、使用Builder的setTitle()或者setCustomTitil()來設置標題。

3、使用Builder的setIcon()來設置圖標

4、使用Builder的相關方法來設置內容

5、使用Builder的setpositivebutton()和setnegativeButton()設置按鈕,setNeutralButton()用於設置中立的按鈕。

6、使用Builder的create()方法創建一個AlertDialog的對象。

7、然後把Dialog對象通過show()方法顯示出來。


AlertDialog.Builder提供了六種指定對話框內容的方法

1、setMessage() 設置對話框內容爲簡單的文本格式。

2、setitems() 設置對話框的內容爲簡單的列表項。

3、setSingleChoiceItems 設置對話框的內容爲單選按鈕的列表項

4、setMultiChoiceitems() 設置對話框內容爲多選按鈕列表項

5、setAdapter() 設置對話框內容爲自定義的列表項

6、setView() 設置對話框內容爲自定義的View

package com.example.toast_demo01;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

@SuppressLint("ShowToast")
public class AlertDialogDemo extends Activity implements OnClickListener {
	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private Button button5;
	private Button button6;
	private Button button7;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.alertaialog);
		initEvent();
	}

	// findViewById集合方法
	public void initEvent() {
		button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(this);
		button2 = (Button) findViewById(R.id.button2);
		button2.setOnClickListener(this);
		button3 = (Button) findViewById(R.id.button3);
		button3.setOnClickListener(this);
		button4 = (Button) findViewById(R.id.button4);
		button4.setOnClickListener(this);
		button5 = (Button) findViewById(R.id.button5);
		button5.setOnClickListener(this);
		button6 = (Button) findViewById(R.id.button6);
		button6.setOnClickListener(this);
		button7 = (Button) findViewById(R.id.button7);
		button7.setOnClickListener(this);
	}

	// 監聽事件
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			simpleDialog();
			break;

		case R.id.button2:
			simpleListDialog();
			break;

		case R.id.button3:
			simpleSingleChoiceDialog();
			break;

		case R.id.button4:
			simpleMultiChoiceDialog();
			break;

		case R.id.button5:
			simpleCustomAdapterDiaglog();
			break;

		case R.id.button6:
			simpleCustomViewDialog();
			break;

		default:
			break;
		}
	}

	// 簡單窗口
	public void simpleDialog() {
		// AlertDialog.Builder不寫出全局:如果全局,會被反覆賦值,這樣容易產生錯誤。
		AlertDialog.Builder builder = new AlertDialog.Builder(
				AlertDialogDemo.this);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("對話框提示信息");
		builder.setMessage("簡單的對話框");

		// 設置第一個按鈕
		builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(AlertDialogDemo.this, "確認", 0).show();
			}
		});

		// 設置中間按鈕
		builder.setNeutralButton("中立", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(AlertDialogDemo.this, "中立", 0).show();
			}
		});

		// 設置最後一個按鈕
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(AlertDialogDemo.this, "取消", 0).show();
			}
		});

		// 顯示對話框關鍵性代碼,沒寫的話對話框不顯示
		builder.create().show();
	}

	// 選項數組
	String items[] = { "三國", "水滸", "紅樓", "西遊" };

	// 簡單列表窗口
	public void simpleListDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				AlertDialogDemo.this);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("列表對話框");

		// 對話框中內容爲列表的寫法
		builder.setItems(items, new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(AlertDialogDemo.this, items[which], 0).show();
			}
		});
		builder.create().show();
	}

	// 單選按鈕對話框
	int singleIndex = 0;

	public void simpleSingleChoiceDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				AlertDialogDemo.this);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("單選按鈕對話框");
		builder.setSingleChoiceItems(items, 0,
				new DialogInterface.OnClickListener() {
					// 0爲默認選擇的按鈕
					@Override
					public void onClick(DialogInterface dialog, int which) {
						singleIndex = which;
					}
				});
		builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(AlertDialogDemo.this, items[singleIndex], 0)
						.show();
			}
		});

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

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

			}
		});
		builder.create().show();
	}

	// 定義多選選項的初始狀態
	boolean checkedItems[] = { false, false, false, false };
	ArrayList<Integer> myChseList = new ArrayList<Integer>();

	// 多選按鈕對話框
	public void simpleMultiChoiceDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				AlertDialogDemo.this);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("多選按鈕對話框");
		builder.setMultiChoiceItems(items, checkedItems,
				new DialogInterface.OnMultiChoiceClickListener() {

					public void onClick(DialogInterface dialog, int which,
							boolean isChecked) {
						// 改變選中項的狀態
						checkedItems[which] = isChecked;

						if (isChecked) {
							myChseList.add(which);
						} else {
							myChseList.remove(which);
						}
					}
				});

		builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				String multi = "";

				// Toast各個選中項
				for (int i = 0; i < checkedItems.length; i++) {
					if (checkedItems[i] == true) {
						multi = multi + items[i];
						checkedItems[i] = false;
					}
				}
				Toast.makeText(AlertDialogDemo.this, multi, 0).show();
			}
		});

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

			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.create().show();
	}

	// 自定義列表對話框
	public void simpleCustomAdapterDiaglog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				AlertDialogDemo.this);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("自定義對話框");

		builder.setAdapter(new ArrayAdapter<String>(AlertDialogDemo.this,
				android.R.layout.simple_list_item_1, items),
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(AlertDialogDemo.this, items[which], 0)
								.show();
					}
				});
		builder.create().show();
	}

	// 自定義佈局對話框
	@SuppressWarnings("unused")
	private void simpleCustomViewDialog() {
		LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(
				R.layout.dialog_login, null);
		AlertDialog.Builder builder = new AlertDialog.Builder(
				AlertDialogDemo.this);
		builder.setIcon(R.drawable.ic_launcher);
		builder.setTitle("自定義佈局對話框");
		builder.setView(layout);
		builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {

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

			}
		});

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

			public void onClick(DialogInterface dialog, int which) {

			}
		});
		builder.create().show();
	}

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章