Android中几种常用的Dialog

        在开发的过程中需求遇到了各种不同样式的Dialog显示的问题,从网上搜罗了一些资料整理了一下,目的只有两个:1.在以后的开发中不用漫天遍野的找资料了;2.希望可以方便有需要的朋友们。如果有更加权威和丰富的样式或者代码,希望各位能够留言提供宝贵的意见,来丰富这篇文章,方便我们大家,谢谢!


1、AlertDialog    功能最丰富,用途最广泛

// 1 创建一个Builder 

AlertDialog.Builder builder = new AlertDialog.Builder(this);

// 2 给Builder设置属性

builder.setTitle("Java培训");// 设置Dialog的标题

    builder.setMessage("访问OSC的网页");// 设置Dialog的信息

    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {// 设置确定按钮

 

                @Override

                public void onClick(DialogInterface dialog, int which) {

                    Toast.makeText(getApplicationContext(), "访问OSC的网页", 0).show();

                }

            });

    builder.setNeutralButton("隐藏", new DialogInterface.OnClickListener() {

 

        @Override

        public void onClick(DialogInterface dialog, int which) {

        }

    });

    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

        @Override

        public void onClick(DialogInterface dialog, int which) {

        }

    });

//从上我们可以看出Dialog的按钮一般分为了三种,确定builder.setPositiveButton(),隐藏builder.setNeutralButton,取消builder.setNegativeButton

// 3 创建Dialog

    AlertDialog dialog = builder.create();

     

// 4 显示Dialog  

dialog.show();



2、ListDialog   这样子可以让内容以列表的形式显示出来

final String[] items = { "java" , "donet" , "php" };
builder.setItems(items, new DialogInterface.OnClickListener() {
 
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(getApplicationContext(), items[which], 0 ).show();
             }
         });



3、单选框Dialog(singleItemDialog)   这样子可以让内容以单选框的样式显示出来

builder.setSingleChoiceItems(items, 2 , new DialogInterface.OnClickListener() {
 
             @Override
             public void onClick(DialogInterface dialog, int which) {
                 Toast.makeText(getApplicationContext(), items[which], 0 ).show();
             }
         });




4、复选框Dialog     这样可以让内容以多选的形式显示出来

final String[] items = { "java", "donet", "php" };

        final boolean[] checkedItems = new boolean[] { true, false, false };

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setTitle("选择语言");

        builder.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {

 

            @Override

            public void onClick(DialogInterface dialog, int which, boolean isChecked) {

                Toast.makeText(getApplicationContext(), items[which] + isChecked, 0).show();

            }

        });




5、进度条Dialog  

ProgressDialog progressDialog = new ProgressDialog( this );
         progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // 设置为水平进度条
         progressDialog.setTitle( "这是一个圆形进度条对话框" );
         progressDialog.setMax( 100 ); // 设置进度条的最大值
         progressDialog.setCancelable( false ); // 设置回退键失效
 
         progressDialog.setButton( "取消" , new DialogInterface.OnClickListener() {
 
             @Override
             public void onClick(DialogInterface dialog, int which) {
             }
         });
         progressDialog.show();
         progressDialog.setProgress( 50 ); // 设置进度条的当前刻度




注意的是Dialog是activity的一个控件,弹出Dialog的时候,activity是不会失去焦点的,也就是说activity不会调用onPause()方法


6、另外,还有自定义的Dialog。童鞋们可以加载自己想要显示的EditText、布局等等。还可以更换自己想要的背景图片等。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值