Android AlertDialog

本文详细介绍了如何使用 Android 中的 AlertDialog 类来创建多种类型的对话框,包括带有不同按钮配置的简单消息对话框、列表选择对话框、进度条对话框、单选或多选对话框等,并展示了如何自定义布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[align=left]这个AlertDialog和Preference的区别是:一个是临时的一个是文件保存 都有简介直接实现DialogInterface接口

@Override
protected Dialog onCreateDialog(int id) {
super.onCreateDialog(id);
switch (id) {
case DIALOG_YES_NO_MESSAGE:
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_two_buttons_title)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel so do some stuff */
}
})
.create();
case DIALOG_YES_NO_LONG_MESSAGE:
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_two_buttons_msg)
.setMessage(R.string.alert_dialog_two_buttons2_msg)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Something so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

/* User clicked Cancel so do some stuff */
}
})
.create();
case DIALOG_LIST:
return new AlertDialog.Builder(AlertDialogSamples.this)
.setTitle(R.string.select_dialog)
.setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
/* User clicked so do some stuff */
String[] items = getResources().getStringArray(R.array.select_dialog_items);
/*new AlertDialog.Builder(AlertDialogSamples.this)
.setMessage("You selected: " + which + " , " + items[which])
.show();*/
Toast.makeText(AlertDialogSamples.this, "You selected: " + which + " , " + items[which], Toast.LENGTH_LONG).show();
}
})
.create();
case DIALOG_PROGRESS:
mProgressDialog = new ProgressDialog(AlertDialogSamples.this);
mProgressDialog.setIcon(R.drawable.alert_dialog_icon);
mProgressDialog.setTitle(R.string.select_dialog);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setMax(MAX_PROGRESS);
mProgressDialog.setButton(DialogInterface.BUTTON_POSITIVE, getText(R.string.alert_dialog_hide), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(AlertDialogSamples.this, "1You click " + whichButton + "--", Toast.LENGTH_LONG).show();
}
});
mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getText(R.string.alert_dialog_hide), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(AlertDialogSamples.this, "2You click " + whichButton + "--", Toast.LENGTH_LONG).show();
}
});
return mProgressDialog;
case DIALOG_SINGLE_CHOICE:
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_single_choice)
.setSingleChoiceItems(R.array.select_dialog_items2, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String[] items = getResources().getStringArray(R.array.select_dialog_items2);
Toast.makeText(AlertDialogSamples.this, "SCI---You selected: " + whichButton + " , " + items[whichButton], Toast.LENGTH_LONG).show();
}
})
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(AlertDialogSamples.this, "P---You selected: " + whichButton + " , ", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Toast.makeText(AlertDialogSamples.this, "N---You selected: " + whichButton + " , ", Toast.LENGTH_LONG).show();
}
})
.create();
case DIALOG_MULTIPLE_CHOICE:
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.ic_popup_reminder)
.setTitle(R.string.alert_dialog_multi_choice)
.setMultiChoiceItems(R.array.select_dialog_items3,
new boolean[]{false, true, false, true, false, false, false},
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,boolean isChecked) {
String[] items = getResources().getStringArray(R.array.select_dialog_items3);
Toast.makeText(AlertDialogSamples.this, "SCI---You selected: " + whichButton + " , " + items[whichButton], Toast.LENGTH_LONG).show();
}
})
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { // here ------------------------111一111-----------------------
// here ------------------------111一111-----------------------
// here ------------------------111一111-----------------------
// here ------------------------111一111-----------------------
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.create();
case DIALOG_MULTIPLE_CHOICE_CURSOR:
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.SEND_TO_VOICEMAIL
};
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null);
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.ic_popup_reminder)
.setTitle(R.string.alert_dialog_multi_choice_cursor)
.setMultiChoiceItems(cursor,
ContactsContract.Contacts.SEND_TO_VOICEMAIL,
ContactsContract.Contacts.DISPLAY_NAME,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int whichButton,boolean isChecked) {
Toast.makeText(AlertDialogSamples.this,"Readonly Demo Only - Data will not be updated",Toast.LENGTH_SHORT).show();
}
})
.create();
case DIALOG_TEXT_ENTRY:
// This example shows how to add a custom layout to an AlertDialog
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();
}
return null;
}
[/align]

[size=medium][color=red]谁能告诉我怎么在这里(here 111一111)获取这个多选项?[/color][/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值