Android下常见的四种对话框

摘要:在实际开发过程有时为了能够和用户进行很好的交互,需要使用到对话框,在Android中常用的对话框有四种:普通对话框、单选对话框、多选对话框、进度对话框。

一、普度对话框

 1  public void click(View view)
 2    {
 3        //this代表MainActivity是子类    getApplicationContext代表context是父类
 4        //对话框不能用getApplicationContext
 5        AlertDialog.Builder builder = new AlertDialog.Builder(this);//getApplicationContext());
 6        builder.setTitle("警告");
 7        builder.setMessage("世界上最遥远的距离是没有网络");
 8        builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
 9        {
10            @Override
11            public void onClick(DialogInterface dialogInterface, int i)
12            {
13                Toast.makeText(mContext, "点击了确定了按钮", Toast.LENGTH_SHORT).show();
14            }
15        });
16        builder.setNegativeButton("确定", new DialogInterface.OnClickListener()
17        {
18            @Override
19            public void onClick(DialogInterface dialogInterface, int i)
20            {
21                Toast.makeText(mContext, "点击了取消了按钮", Toast.LENGTH_SHORT).show();
22            }
23        });
24        builder.show();
25    }

二、单选对话框

   public void click(View view)
    {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);//getApplicationContext());
        builder.setTitle("请选择你喜欢的课");
        final String item[] = {"android", "java", "c", "c++"};
        builder.setSingleChoiceItems(item, -1, new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialogInterface, int i)
            {
                String s = item[i];
                Toast.makeText(mContext, s, Toast.LENGTH_SHORT).show();
                dialogInterface.dismiss();
            }
        });
        builder.show();
    }

三、多选对话框

 public void click(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);//getApplicationContext());
        builder.setTitle("请选出你喜欢的水果");
        final String[] item = {"苹果", "榴莲", "荔枝", "黄瓜", "香蕉"};
        final boolean[] checkChoisce = {true, false, false, false, true};
        builder.setMultiChoiceItems(item, checkChoisce, new DialogInterface.OnMultiChoiceClickListener()
        {
            @Override
            public void onClick(DialogInterface dialogInterface, int i, boolean b)
            {
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialogInterface, int i)
            {
                Toast.makeText(mContext, "点击了确定了按钮", Toast.LENGTH_SHORT).show();
                StringBuffer sb = new StringBuffer();
                for (int j = 0; j < item.length; j ++)
                {
                    if (checkChoisce[j])
                    {
                        String s = item[j];
                        sb.append(s);
                    }
                }
                Toast.makeText(mContext, sb.toString(), Toast.LENGTH_SHORT).show();
                dialogInterface.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialogInterface, int i)
            {
                Toast.makeText(mContext, "点击了取消了按钮", Toast.LENGTH_SHORT).show();
            }
        });
        builder.show();
    }

四、进度对话框

   public void click(View view)
    {
        //与进度相关的UI都可以在子线程更新UI
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setTitle("玩命加载中");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();
        new Thread(){public void run(){
            for (int i = 0; i < 100; i ++)
            {
                SystemClock.sleep(50);
                dialog.setProgress(i);
            }
            dialog.dismiss();
        }}.start();
    }

 

转载于:https://www.cnblogs.com/doitbyyourself/p/6260390.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值