Android day_11 (四种对话框)

 Android day_11 (四种对话框)_第1张图片

//弹出一个普通对话框
    public void click1(View view) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("警告");
        builder.setMessage("没有网络");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                System.out.println("点击了确定");
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                System.out.println("点击了取消");
            }
        });
        //最后一步一定要记得和Toast一样要show出来
        builder.show();
    }

    //弹出一个单选对话框
    public void click2(View view) {
        //通过Builder 构造器来构造
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择");
        final String[] items = {"Android", "ios", "C", "C++", "html", "C#"};
        //第一个String[]  第一个写-1代表没有条目被选中
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //把选中的条目取出
                String item = items[i];
                Toast.makeText(getApplicationContext(), "选择的是" + item, Toast.LENGTH_LONG).show();
                //关闭对话框
                dialogInterface.dismiss();
            }
        });

        //最后一步一定要记得和Toast一样要show出来
        builder.show();
    }

    //弹出一个多选对话框
    public void click3(View view) {
        //通过Builder 构造器来构造
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择");
        final String[] items = {"Android", "ios", "C", "C++", "html", "C#"};
        final boolean[] c = {true, false, false, false, false, false};
        //第一个String[]  第二个是一个 boolean[]代表默认选中的
        builder.setMultiChoiceItems(items, c, 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) {
                //取出选中的
                StringBuffer sb = new StringBuffer();
                for (int in = 0; in < c.length; in++) {
                    //判断选中
                    if (c[in]) {
                        String fruit = items[in];
                        sb.append(fruit + "  ");
                    }
                }
                Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_LONG).show();
                dialogInterface.dismiss();
            }
        });

        //最后一步一定要记得和Toast一样要show出来
        builder.show();
    }

    //弹出一个进度条对话框
    public void click4(View view) {
        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setTitle("正在玩命加载中……");
        //设置进度条样式
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.show();
        //关于进度条的UI可以直接在子线程更新UI
        new Thread() {
            public void run() {
                //设置最大进度
                dialog.setMax(100);
                //设置当前进度
                for (int i = 0; i <= 100; i++) {
                    dialog.setProgress(i);
                    //睡眠50ms
                    SystemClock.sleep(50);
                }
                dialog.dismiss();
            }
        }.start();
    }

 

你可能感兴趣的:(Android)