Dialogs

Dialogs是一个弹出来的小的窗口,让用户做决定或者输入额外的信息。一个对话框不会暂满整个屏幕,通常被用来一些需要用户在他们执行前动作的事件。
Dailog是基本的父类,但是我们应该避免直接实例化它,替代的,使用它的子类。
第一个是AlertDialog,一个可以展示标题,最多三个按钮,一些选项,或者一个自定义的布局文件。
官方网站推荐使用DialogFragment,方便管理,这个要学习Fragment之后在好好研究,给的例子的继承DialogFragment,其实和疯狂安卓差不多,就是然后builder都差不多,调用show方法,显示。另外又有关于兼容的问题,真的麻烦啊。
创建一个Alert Dialog.
组成
1. Title,如果内容区不是很复杂的信息,list或者自定义布局,一般不需要,如果仅仅是提醒的,就不用了
2. Content Area 展示信息、list或者自定义布局文件
3. Action Buttons 最多三个按钮
 


创建一个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();


创建了之后需要添加button,使用set…方法,需要一个String resource设置标题,和一个DialogInterface.OnClickListener事件,处理用户点击。
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();
按钮有三种:Positive 确定,Negative cancle  Netural, 比如下次提醒,我们比较常见的,一些更新提示,就是这么做的吧。
这三种按钮,如果有,只能有一个。


添加一个List
三种可用的list: 1.传统的单选 2.persistent的单选(radio buttons)不太理解  3.persistent多选的(checkboxes)
使用setItems


.setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item
           }
});
因为lsit展示在内容区,不能同时显示message和list,所以需要调用setTitle设置标题。作为替换,也可以使用setAdapter,动态加载,如果我们要这么用,最好用loader,这个后续继续学习啊。
If you choose to back your list with a ListAdapter, always use a Loader so that the content loads asynchronously. This is described further in Building Layouts with an Adapter and the Loaders guide.


默认我们点击任意一个item,就取消这个Altert Dialog了,
使用setMultiChoiceItems() or setSingleChoiceItems() 记住用户的选择吧。
给了一个例子,如果以后用户在点击,要给用户展示他的选择


创建自定义的布局:

需要调用setView方法,默认view会占满这个Dialog,不过我们可以使用setTitle和button,添加上去


默认我们点击Dialog的item,这个Dialog会自动消失,除非是多选或者单选的按钮,后续处理。

等学完Fragment之后在回来学习,第二次看一遍。









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值