Dialog总结(一)

在android api中有很多控件都继承自view的,但是dialog并不是继承自view的,还有anctivity,这里简单的只讲下dialog

 

dialog直接继承自Object类。

 

它有如下构造函数:

 

Dialog(Context context)

Dialog(Context context, int theme)

 

Dialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener)

 

一般用的最多的就是第一个了,还有它的show()函数  cancel()  dismiss()

 

使用cancel dismiss 会关闭dialog,但是不会清理资源,还在堆里,必须清理,否则会出现问题的

 

它的子类有AlertDialog,CharacterPickerDialog,这个CharacterPickerDialog我没有用过

 

一般我使用AlertDialog,而且不是直接创建对象,使用它的一个内部类来返回实例

 

 

onCreateDialog()函数:

protected Dialog onCreateDialog(int id) {
  switch (id) {
  case 1: {
   return new AlertDialog.Builder(this).setIcon(R.drawable.icon)
     .setTitle(R.string.menu_group_longclick).setItems(
       R.array.menu_group_longclick_array,
       new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
          int which) {
         onMenuItemClick(
           Globals.DIALOG_GROUPS_LONGCLICK,
           which);
         // removeDialog(1);
         //建议使用,清楚缓存
        }
       }).create();
    }
   case 2:{
   
    return new AlertDialog.Builder(this).setIcon(R.drawable.icon)
     .setTitle(R.string.menu_group_longclick).setItems(
       R.array.menu_group_longclick_array,
       new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,
          int which) {
         onMenuItemClick(
           Globals.DIALOG_GROUPS_LONGCLICK,
           which);
           //建议使用,清楚缓存
         // removeDialog(2);
        }
       }).create();
   }
  }
}

 

然后使用showDialog(1  or  2)弹出Dialog,它有两个按钮,是源代码里写好的,风格也可以自定义,也可以将他们设置为false:

 

dialog.getButton(AlertDialog.Button_POSITIVE).setEnable(false);

 

 

AlertDialog有三个子类:DatePickerDialog   TimePickerDialog  ProgressDialog ,同样也可在oncreateDialog(int id)里面返回实例

 

case Globals.DIALOG_MYINFO_PROGRESS :{

mProgressDialog = new ProgressDialog(this);

mProgressDialog.setMessage(getString(R.string.msg_save_progress));

return mProgressDialog;

}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值