您的位置:首页 > 移动开发 > Android开发

android学习之AlertDialog使用

2016-03-05 21:19 597 查看
原文地址:http://developer.android.com/intl/zh-cn/guide/topics/ui/dialogs.html

创建AlertDialog

// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

// 2. Chain together various setter methods to set the dialog characteristics
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);

// 3. Get the AlertDialog from create()
AlertDialog dialog = builder.create();


添加如图所示的按钮



AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Add the buttons
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Set other dialog properties
...

// Create the AlertDialog
AlertDialog dialog = builder.create();


set…Button() 方法需要一个按钮标题(由字符串资源提供)和一个 DialogInterface.OnClickListener,后者用于定义用户按下该按钮时执行的操作。

您可以添加三种不同的操作按钮:

肯定

您应该使用此按钮来接受并继续执行操作(“确定”操作)。

否定

您应该使用此按钮来取消操作。

中性

您应该在用户可能不想继续执行操作,但也不一定想要取消操作时使用此按钮。 它出现在肯定按钮和否定按钮之间。 例如,实际操作可能是“稍后提醒我”。

对于每种按钮类型,您只能为 AlertDialog 添加一个该类型的按钮。也就是说,您不能添加多个“肯定”按钮。

添加列表

可通过 AlertDialog API 提供三种列表:

- 传统单选列表

- 永久性单选列表(单选按钮)

- 永久性多选列表(复选框)

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: