android学习之AlertDialog使用

来源:互联网 发布:田柾国金泰亨感情知乎 编辑:程序博客网 时间:2024/06/07 10:24

原文地址:http://developer.android.com/intl/zh-cn/guide/topics/ui/dialogs.html

创建AlertDialog

// 1. Instantiate an AlertDialog.Builder with its constructorAlertDialog.Builder builder = new AlertDialog.Builder(getActivity());// 2. Chain together various setter methods to set the dialog characteristicsbuilder.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 buttonsbuilder.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 AlertDialogAlertDialog dialog = builder.create();

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

肯定
您应该使用此按钮来接受并继续执行操作(“确定”操作)。
否定
您应该使用此按钮来取消操作。
中性
您应该在用户可能不想继续执行操作,但也不一定想要取消操作时使用此按钮。 它出现在肯定按钮和否定按钮之间。 例如,实际操作可能是“稍后提醒我”。
对于每种按钮类型,您只能为 AlertDialog 添加一个该类型的按钮。也就是说,您不能添加多个“肯定”按钮。


添加列表

可通过 AlertDialog API 提供三种列表:
- 传统单选列表
- 永久性单选列表(单选按钮)
- 永久性多选列表(复选框)
这里写图片描述

0 0
原创粉丝点击