最簡單的用法(詳見注釋)
// 1列牺、創(chuàng)建簡單的AlertDialog // AlertDialog的構(gòu)造方法全部是Protected的,
// 所以不能直接通過new一個AlertDialog來創(chuàng)建出一個AlertDialog; //
// (1)要創(chuàng)建一個AlertDialog,就要用到AlertDialog.Builder
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
// (2)設(shè)置各種屬性 // 注:不設(shè)置哪項屬性慷蠕,這個屬性就默認(rèn)不會顯示出來
dialog.setTitle("這是一個簡單的對話框");
dialog.setIcon(R.drawable.dictation2_64);
dialog.setMessage("歡迎使用!");
// (3)設(shè)置dialog可否被取消
dialog.setCancelable(true);
// (4)顯示出來
dialog.show();
效果如下:
帶按鈕的AlertDialog
// 2碴犬、帶按鈕的AlertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("確認(rèn)");
dialog.setIcon(R.drawable.dictation2_64);
dialog.setMessage("確定要刪除此項嗎胡嘿?");
// 設(shè)置“確定”按鈕,使用DialogInterface.OnClickListener接口參數(shù)
dialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "點擊了“確認(rèn)”按鈕");
}
});
// 設(shè)置“查看詳情”按鈕,使用DialogInterface.OnClickListener接口參數(shù)
dialog.setNeutralButton("查看詳情",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "點擊了“查看詳情”按鈕");
}
});
// 設(shè)置“取消”按鈕,使用DialogInterface.OnClickListener接口參數(shù)
dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "點擊了“取消”按鈕");
}
});
dialog.show();
效果如下:
類似于ListView的AlertDialog
用setItems(CharSequence[] items, final OnClickListener listener)
方法來實現(xiàn)類似ListView的AlertDialog:
// 3、類似ListView的AlertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("請選擇一項運動");
dialog.setIcon(R.drawable.dictation2_64);
// 設(shè)置為false說明當(dāng)前dialog是不能用返回鍵取消的
dialog.setCancelable(false);
// 列表字符串?dāng)?shù)組
final String[] sportsArray = new String[] { "跑步", "籃球", "游泳",
"自行車", "羽毛球" };
// 用于在item的點擊事件中照棋,記錄選擇的是哪一項,初始值設(shè)為0.這里用final數(shù)組只是因為匿名內(nèi)部類中只能使用外部終態(tài)的變量
final int selectedIndex[] = { 0 };
// 用setItems方法來實現(xiàn)
dialog.setItems(sportsArray, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedIndex[0] = which;
Log.d("Dialog", "選擇了:" + sportsArray[selectedIndex[0]]);
}
});
dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "點擊了“取消”按鈕");
}
});
dialog.show();
效果如下:
類似RadioButton的AlertDialog
用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)
方法來實現(xiàn)類似RadioButton的AlertDialog武翎。
第一個參數(shù)是要顯示的數(shù)據(jù)的數(shù)組烈炭,第二個參數(shù)是初始值(初始被選中的item),第三個參數(shù)是點擊某個item的觸發(fā)事件宝恶。
// 4符隙、類似RadioButton的AlertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("請選擇一項運動");
dialog.setIcon(R.drawable.dictation2_64);
// 列表字符串?dāng)?shù)組
final String[] sportsArray = new String[] { "跑步", "籃球", "游泳",
"自行車", "羽毛球" };
// 用于在item的點擊事件中,記錄選擇的是哪一項垫毙,初始值設(shè)為0.這里用final數(shù)組只是因為匿名內(nèi)部類中只能使用外部終態(tài)的變量
final int selectedIndex[] = { 0 };
// 用setSingleChoiceItems方法來實現(xiàn)
dialog.setSingleChoiceItems(sportsArray, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedIndex[0] = which;
}
});
dialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "選擇了:"
+ sportsArray[selectedIndex[0]]);
}
});
dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "點擊了“取消”按鈕");
}
});
dialog.show();
效果如下:
類似CheckBox的AlertDialog
用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)
方法來實現(xiàn)類似CheckBox的AlertDialog霹疫,第一個參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個參數(shù)是選中狀態(tài)的數(shù)組综芥,第三個參數(shù)是點擊某個item的觸發(fā)事件丽蝎。
// 5、類似CheckBox的AlertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("請選擇喜歡的運動(可多選)");
dialog.setIcon(R.drawable.dictation2_64);
// 列表字符串?dāng)?shù)組
final String[] sportsArray = new String[] { "跑步", "籃球", "游泳",
"自行車", "羽毛球" };
// 用于在item的點擊事件中膀藐,記錄選擇了哪些項.
final boolean[] selectedIndex = { true, true, false, false, false };
// 用setMultiChoiceItems方法來實現(xiàn)
dialog.setMultiChoiceItems(sportsArray, selectedIndex,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
selectedIndex[which] = isChecked;
}
});
dialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
for (int i = 0; i < selectedIndex.length; i++) {
if (selectedIndex[i]) {
Log.d("Dialog", "選擇了:" + sportsArray[i]);
}
}
}
});
dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d("Dialog", "點擊了“取消”按鈕");
}
});
dialog.show();
效果如下:
自定義View的AlertDialog
有時候系統(tǒng)自帶的AlertDialog風(fēng)格不能滿足我們的需求屠阻,就比如說我們要實現(xiàn)一個Login畫面,有用戶名和密碼消请,這時我們就要用到自定義View的AlertDialog栏笆,步驟如下:
- 先創(chuàng)建自定義登錄框的布局文件
my_login_view.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="賬號:" />
<EditText
android:id="@+id/my_login_account_et"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密碼:" />
<EditText
android:id="@+id/my_login_password_et"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberPassword" />
</LinearLayout>
</LinearLayout>
- 在Activity的合適地方創(chuàng)建自定義的AlertDialog(比如按鈕的點擊事件中):
// 6、自定義View的AlertDialog
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("用戶登錄");
// 取得自定義View
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View myLoginView = layoutInflater.inflate(
R.layout.my_login_view, null);
dialog.setView(myLoginView);
dialog.setPositiveButton("確定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText loginAccountEt = (EditText) myLoginView
.findViewById(R.id.my_login_account_et);
EditText loginPasswordEt = (EditText) myLoginView
.findViewById(R.id.my_login_password_et);
Log.d("MyLogin Dialog", "輸入的用戶名是:"
+ loginAccountEt.getText().toString());
Log.d("MyLogin Dialog", "輸入的密碼是:"
+ loginPasswordEt.getText().toString());
}
});
dialog.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.show();
效果如下:
點擊“確定”按鈕后LogCat中的內(nèi)容: