Android的AlertDialog詳解
發(fā)布時(shí)間:2012-10-09 16:59:29 作者:佚名
我要評(píng)論

本文對(duì)Android的AlertDialog進(jìn)行詳細(xì)了解
AlertDialog的構(gòu)造方法全部是Protected的,所以不能直接通過(guò)new一個(gè)AlertDialog來(lái)創(chuàng)建出一個(gè)AlertDialog。
要?jiǎng)?chuàng)建一個(gè)AlertDialog,就要用到AlertDialog.Builder中的create()方法。
使用AlertDialog.Builder創(chuàng)建對(duì)話框需要了解以下幾個(gè)方法:
setTitle :為對(duì)話框設(shè)置標(biāo)題
setIcon :為對(duì)話框設(shè)置圖標(biāo)
setMessage:為對(duì)話框設(shè)置內(nèi)容
setView : 給對(duì)話框設(shè)置自定義樣式
setItems :設(shè)置對(duì)話框要顯示的一個(gè)list,一般用于顯示幾個(gè)命令時(shí)
setMultiChoiceItems :用來(lái)設(shè)置對(duì)話框顯示一系列的復(fù)選框
setNeutralButton :普通按鈕
setPositiveButton :給對(duì)話框添加"Yes"按鈕
setNegativeButton :對(duì)話框添加"No"按鈕
create : 創(chuàng)建對(duì)話框
show :顯示對(duì)話框
一、簡(jiǎn)單的AlertDialog
下面,創(chuàng)建一個(gè)簡(jiǎn)單的ALertDialog并顯示它:
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("對(duì)話框的標(biāo)題").
setMessage("對(duì)話框的內(nèi)容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("對(duì)話框的標(biāo)題").
setMessage("對(duì)話框的內(nèi)容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:

二、帶按鈕的AlertDialog
上面的例子很簡(jiǎn)單,下面我們?cè)谶@個(gè)AlertDialog上面加幾個(gè)Button,實(shí)現(xiàn)刪除操作的提示對(duì)話框
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("確定刪除?").
setMessage("您確定刪除該條信息嗎?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看詳情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("確定刪除?").
setMessage("您確定刪除該條信息嗎?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看詳情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}在這個(gè)例子中,我們定義了三個(gè)按鈕,分別是"Yes"按鈕,"No"按鈕以及一個(gè)普通按鈕,每個(gè)按鈕都有onClick事件,TODO的地方可以放點(diǎn)了按鈕之后想要做的一些處理
看一下運(yùn)行結(jié)果:

可以看到三個(gè)按鈕添加到了AlertDialog上,三個(gè)沒(méi)有添加事件處理的按鈕,點(diǎn)了只是關(guān)閉對(duì)話框,沒(méi)有任何其他操作。
三、類似ListView的AlertDialog
用setItems(CharSequence[] items, final OnClickListener listener)方法來(lái)實(shí)現(xiàn)類似ListView的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setItems(arrayFruit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setItems(arrayFruit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:

四、類似RadioButton的AlertDialog
用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法來(lái)實(shí)現(xiàn)類似RadioButton的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是初始值(初始被選中的item),第三個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件
在這個(gè)例子里面我們?cè)O(shè)了一個(gè)selectedFruitIndex用來(lái)記住選中的item的index
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
private int selectedFruitIndex = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
private int selectedFruitIndex = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
運(yùn)行結(jié)果如下:

五、類似CheckBox的AlertDialog
用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法來(lái)實(shí)現(xiàn)類似CheckBox的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是選中狀態(tài)的數(shù)組,第三個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:

六、自定義View的AlertDialog
有時(shí)候我們不能滿足系統(tǒng)自帶的AlertDialog風(fēng)格,就比如說(shuō)我們要實(shí)現(xiàn)一個(gè)Login畫面,有用戶名和密碼,這時(shí)我們就要用到自定義View的AlertDialog
先創(chuàng)建Login畫面的布局文件
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" />
<EditText
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" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" />
<EditText
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" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
然后在Activity里面把Login畫面的布局文件添加到AlertDialog上
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得自定義View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用戶登錄").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登錄", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得自定義View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用戶登錄").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登錄", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:

摘自 殤雲(yún)的專欄
要?jiǎng)?chuàng)建一個(gè)AlertDialog,就要用到AlertDialog.Builder中的create()方法。
使用AlertDialog.Builder創(chuàng)建對(duì)話框需要了解以下幾個(gè)方法:
setTitle :為對(duì)話框設(shè)置標(biāo)題
setIcon :為對(duì)話框設(shè)置圖標(biāo)
setMessage:為對(duì)話框設(shè)置內(nèi)容
setView : 給對(duì)話框設(shè)置自定義樣式
setItems :設(shè)置對(duì)話框要顯示的一個(gè)list,一般用于顯示幾個(gè)命令時(shí)
setMultiChoiceItems :用來(lái)設(shè)置對(duì)話框顯示一系列的復(fù)選框
setNeutralButton :普通按鈕
setPositiveButton :給對(duì)話框添加"Yes"按鈕
setNegativeButton :對(duì)話框添加"No"按鈕
create : 創(chuàng)建對(duì)話框
show :顯示對(duì)話框
一、簡(jiǎn)單的AlertDialog
下面,創(chuàng)建一個(gè)簡(jiǎn)單的ALertDialog并顯示它:
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("對(duì)話框的標(biāo)題").
setMessage("對(duì)話框的內(nèi)容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("對(duì)話框的標(biāo)題").
setMessage("對(duì)話框的內(nèi)容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:

二、帶按鈕的AlertDialog
上面的例子很簡(jiǎn)單,下面我們?cè)谶@個(gè)AlertDialog上面加幾個(gè)Button,實(shí)現(xiàn)刪除操作的提示對(duì)話框
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("確定刪除?").
setMessage("您確定刪除該條信息嗎?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看詳情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("確定刪除?").
setMessage("您確定刪除該條信息嗎?").
setIcon(R.drawable.ic_launcher).
setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNeutralButton("查看詳情", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}在這個(gè)例子中,我們定義了三個(gè)按鈕,分別是"Yes"按鈕,"No"按鈕以及一個(gè)普通按鈕,每個(gè)按鈕都有onClick事件,TODO的地方可以放點(diǎn)了按鈕之后想要做的一些處理
看一下運(yùn)行結(jié)果:

可以看到三個(gè)按鈕添加到了AlertDialog上,三個(gè)沒(méi)有添加事件處理的按鈕,點(diǎn)了只是關(guān)閉對(duì)話框,沒(méi)有任何其他操作。
三、類似ListView的AlertDialog
用setItems(CharSequence[] items, final OnClickListener listener)方法來(lái)實(shí)現(xiàn)類似ListView的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setItems(arrayFruit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setItems(arrayFruit, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[which], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:

四、類似RadioButton的AlertDialog
用setSingleChoiceItems(CharSequence[] items, int checkedItem, final OnClickListener listener)方法來(lái)實(shí)現(xiàn)類似RadioButton的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是初始值(初始被選中的item),第三個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件
在這個(gè)例子里面我們?cè)O(shè)了一個(gè)selectedFruitIndex用來(lái)記住選中的item的index
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
private int selectedFruitIndex = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
private int selectedFruitIndex = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setSingleChoiceItems(arrayFruit, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectedFruitIndex = which;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(Dialog_AlertDialogDemoActivity.this, arrayFruit[selectedFruitIndex], Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
運(yùn)行結(jié)果如下:

五、類似CheckBox的AlertDialog
用setMultiChoiceItems(CharSequence[] items, boolean[] checkedItems, final OnMultiChoiceClickListener listener)方法來(lái)實(shí)現(xiàn)類似CheckBox的AlertDialog
第一個(gè)參數(shù)是要顯示的數(shù)據(jù)的數(shù)組,第二個(gè)參數(shù)是選中狀態(tài)的數(shù)組,第三個(gè)參數(shù)是點(diǎn)擊某個(gè)item的觸發(fā)事件
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final String[] arrayFruit = new String[] { "蘋果", "橘子", "草莓", "香蕉" };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("你喜歡吃哪種水果?").
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + "、");
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:

六、自定義View的AlertDialog
有時(shí)候我們不能滿足系統(tǒng)自帶的AlertDialog風(fēng)格,就比如說(shuō)我們要實(shí)現(xiàn)一個(gè)Login畫面,有用戶名和密碼,這時(shí)我們就要用到自定義View的AlertDialog
先創(chuàng)建Login畫面的布局文件
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" />
<EditText
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" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/user" />
<EditText
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" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/passward" />
<EditText
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
然后在Activity里面把Login畫面的布局文件添加到AlertDialog上
[java] package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得自定義View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用戶登錄").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登錄", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
package com.tianjf;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
public class Dialog_AlertDialogDemoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得自定義View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用戶登錄").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登錄", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}運(yùn)行結(jié)果如下:

摘自 殤雲(yún)的專欄
相關(guān)文章
輕松玩轉(zhuǎn)虛擬機(jī)套娃! 在Hyper-V中開啟嵌套虛擬化的技巧
有辦法在虛擬機(jī)中創(chuàng)建虛擬機(jī)嗎?答案是肯定的,在虛擬機(jī)中運(yùn)行虛擬機(jī)是可能的,我們將其稱為嵌套虛擬化,下面我們就來(lái)看看詳細(xì)操作方法2025-02-19龍蜥操作系統(tǒng)Anolis OS-23.x安裝配置圖解教程(保姆級(jí))
本文主要介紹了安裝和配置AnolisOS 23.2系統(tǒng),包括分區(qū)、軟件選擇、設(shè)置root密碼、網(wǎng)絡(luò)配置、主機(jī)名設(shè)置和禁用SELinux的步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2025-01-20VMware虛擬機(jī)怎么復(fù)制/克隆/導(dǎo)出VMDK文件?
在處理文件拷貝復(fù)制到VMware虛擬機(jī)中的任務(wù)時(shí),有多種方法可以實(shí)現(xiàn)文件的高效傳輸,使用這些方法,可以在不同的操作系統(tǒng)和環(huán)境下,實(shí)現(xiàn)文件的快速、安全遷移,詳細(xì)請(qǐng)看下文2024-09-24統(tǒng)信 UOS V20 桌面專業(yè)版更新發(fā)布:附更新內(nèi)容匯總
統(tǒng)信 UOS V20 桌面專業(yè)版(1070u1)正式發(fā)布,支持微信掃碼登錄、應(yīng)用窗口縮略圖,海量功能修復(fù),詳細(xì)請(qǐng)看下文介紹2024-08-08怎么看自己IP地址? 不同的操作系統(tǒng)中查看路由器的IP地址的方法
在互聯(lián)網(wǎng)上進(jìn)行網(wǎng)絡(luò)連接,每個(gè)設(shè)備都有一個(gè)唯一的標(biāo)識(shí),即IP地址,本文介紹了如何在不同平臺(tái)上查找路由器的IP地址,包括Windows、Mac、iPhone、iPad、Android、Chrome OS和2024-07-16mware虛擬機(jī)運(yùn)行速度卡慢原因分析及解決辦法
很多人在使用虛擬機(jī)系統(tǒng)的時(shí)候,經(jīng)常對(duì)虛擬機(jī)的運(yùn)行速度不甚滿意,甚至經(jīng)常很惱火,虛擬機(jī)速度慢有很多原因,每個(gè)人需要根據(jù)自己的情況具體分析,本文根據(jù)筆者的使用經(jīng)驗(yàn)將2024-02-21Vmware虛擬機(jī)如何與主機(jī)之間直接復(fù)制粘貼文件?
使用虛擬機(jī)的時(shí)候,遇到了錯(cuò)誤,想要把錯(cuò)誤的代碼或是運(yùn)行情況復(fù)制粘貼到Windows的網(wǎng)上論壇區(qū)求助大神,該怎么操作呢?詳細(xì)請(qǐng)看下文介紹2024-02-21統(tǒng)信UOS V20桌面專業(yè)版(1060)11月更新發(fā)布(附更新內(nèi)容匯總)
國(guó)產(chǎn)桌面操作系統(tǒng)統(tǒng)信 UOS V20 專業(yè)版(1060)11月更新發(fā)布,這是統(tǒng)信 UOS V20 專業(yè)版(1060)2023 年度最后一次更新,下面我們就來(lái)看看詳細(xì)的更新內(nèi)容2023-12-01統(tǒng)信 UOS 將推 Rust 版 Bash 命令行工具 utshell(附下載地址)
統(tǒng)信 UOS 服務(wù)器版將于不久后推出 Rust 版 Bash,名為 utshell,旨在突破傳統(tǒng) Shell 所面臨的安全限制,詳細(xì)請(qǐng)看下文介紹2023-09-20UOS開機(jī)進(jìn)入busybox界面怎么辦 uos無(wú)法進(jìn)入桌面的解決辦法
UOS開機(jī)進(jìn)入busybox界面怎么辦?UOS系統(tǒng)開機(jī)黑屏進(jìn)入busybox界面了,該怎么辦呢?下面我們就來(lái)看看uos無(wú)法進(jìn)入桌面的解決辦法2023-09-06