android 中Dialog对话框及自定义Dialog的方法


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 重写方法,让他返回Dialog。
     // 以下是优化代码的替换,只创建一次,每次点击使用showDialog()的方法,不用每次都创建新的,可以减少内存使用
     @Override
     protected  Dialog onCreateDialog( int  id)
     {
         // 因为返回值都是builder,所以可以直接使用.set的方法
         return  new  AlertDialog.Builder(MainActivity. this ).setTitle( "dialog" )
                 .setMessage( "对话框内容" )
                 // dialog里面设置三个按钮,可以关闭对话框
                 .setPositiveButton( "确定" new  DialogInterface.OnClickListener()
                 {
                     @Override
                     public  void  onClick(DialogInterface dialog,  int  which)
                     {
                     }
                 })
                 .setNegativeButton( "取消" new  DialogInterface.OnClickListener()
                 {
                     @Override
                     public  void  onClick(DialogInterface dialog,  int  which)
                     {
                     }
                 }).setNeutralButton( "应用" new  DialogInterface.OnClickListener()
                 {
                     @Override
                     public  void  onClick(DialogInterface dialog,  int  which)
                     {
                     }
                 }).create();
     }


自定义Dialog的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Override
@Deprecated
protected  Dialog onCreateDialog( int  id)
{
     final  Dialog dialog =  new  Dialog( this );
     //设置自定义dialog
     dialog.setContentView(R.layout.dialog);
     dialog.findViewById(R.id.button2).setOnClickListener( new  OnClickListener()
     {
                     
         @Override
         public  void  onClick(View v)
         {
             Toast.makeText(MainActivity. this "定制" , Toast.LENGTH_LONG).show();
             //关闭对话框
             dialog.dismiss();
         }
     });
     return  dialog;
}

同样也是使用showDialog(0)的方法调用它。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值