AlertDialog的使用

1.Alertdialog的几种形式:

 

2.第一种:简单对话框

复制代码
 AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        localBuilder.setTitle("简单对话框");
        localBuilder.setIcon(R.mipmap.ic_launcher);
        localBuilder.setMessage("提示信息?");
        localBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
               /**
                * 确定操作
                * */
            }
        });
        localBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                /**
                 * 确定操作
                 * */
            }
        });

        /***
         * 设置点击返回键不会消失
         * */
       localBuilder.setCancelable(false).create();

        localBuilder.show();
复制代码

3.第二种:列表式对话框

复制代码
   AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        final String[] arrayOfString = { "选项1", "选项2",  "选项3", "选项4", "选项5", "选项6" };
        localBuilder.setTitle("简单列表对话框").setIcon(R.mipmap.ic_launcher).setItems(arrayOfString, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                /**
                 * 操作
                 * */
                Toast.makeText(MainActivity.this, "你选择了: " + arrayOfString[paramAnonymousInt], Toast.LENGTH_SHORT).show();
                /**
                 * 列表对话框不加这句,点击选择后也后不会消失
                 * */
                paramAnonymousDialogInterface.dismiss();
            }
        }).create().show();
复制代码

4.第三种形式:单选对话框

复制代码
 AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        final String[] arrayOfString = {  "1", "2", "3", "4", "5", "6"};
        localBuilder.setTitle("单选对话框").setIcon(R.mipmap.ic_launcher);
        localBuilder.setSingleChoiceItems(arrayOfString, 3, new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                /**
                 * 操作
                 * */
                Toast.makeText(MainActivity.this, "你选择了: " + arrayOfString[paramAnonymousInt], Toast.LENGTH_SHORT).show();

                paramAnonymousDialogInterface.dismiss();
            }
        }).setCancelable(false).create().show();
复制代码

5.第四种形式:多选对话框

复制代码
   AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        final String[] arrayOfString = { "0", "1", "2", "3", "4" };
        localBuilder.setTitle("多选对话框").setIcon(R.mipmap.ic_launcher);
        localBuilder.setMultiChoiceItems(arrayOfString, new boolean[] { true, true, true, false, true }, new DialogInterface.OnMultiChoiceClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt, boolean paramAnonymousBoolean)
            {
                if (paramAnonymousBoolean) {
                    Toast.makeText(MainActivity.this, "你选择了: " + arrayOfString[paramAnonymousInt], Toast.LENGTH_SHORT).show();

                }
            }
        }).setPositiveButton("提交", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                paramAnonymousDialogInterface.dismiss();
            }
        }).create().show();
复制代码

6.第五种形式:自定义对话框

 

复制代码
  AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        localBuilder.setTitle("自定义列表对话框").setIcon(R.mipmap.ic_launcher);
        localBuilder.setView(getLayoutInflater().inflate(R.layout.layout, null));
        localBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                /**
                 *
                 * 操作
                 * */
            }
        }).setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                /**
                 *
                 * 操作
                 * */
            }
        }).create().show();
复制代码

自定义列表对话框:

复制代码
 AlertDialog.Builder localBuilder = new AlertDialog.Builder(this);
        final String[] arrayOfString = { "0", "1", "2", "3", "4", "5", "6", "7", "8" };
        localBuilder.setTitle("自定义列表对话框").setIcon(R.mipmap.ic_launcher);
        localBuilder.setAdapter(new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item, arrayOfString), new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface paramAnonymousDialogInterface, int paramAnonymousInt)
            {
                Toast.makeText(MainActivity.this, "你选择了 :  " + arrayOfString[paramAnonymousInt], Toast.LENGTH_SHORT).show();
            }
        }).create().show();
复制代码

 <AlertDialog 主题颜色>

  传统主题:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_TRADITIONAL)

     深黑色主题:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_HOLO_DARK);

 蓝色主题:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_HOLO_LIGHT);

  深色主题:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_DARK);

浅蓝主题:

AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this,AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值