Android_对话框

效果



代码

    //创建对话框
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    //设置图标
    builder.setIcon(R.mipmap.ic_launcher);
    //设置标题
    builder.setTitle("确定退出!");
    //设置确定按钮
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "点击了确定", Toast.LENGTH_LONG).show();
        }
    });

    //设置取消按钮 如果只是要取消对话框 点击事件可以传NULL
    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, "点击了取消", Toast.LENGTH_LONG).show();
        }
    });
    //显示对话框
    builder.show();

多按钮

效果

代码

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

builder.setIcon(R.mipmap.ic_launcher);

builder.setTitle("提示");

builder.setMessage("是否保存文件?");

builder.setPositiveButton("是", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialogInterface, int i) {

        Toast.makeText(MainActivity.this, "是", Toast.LENGTH_LONG).show();

    }

});
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialogInterface, int i) {

        Toast.makeText(MainActivity.this, "否", Toast.LENGTH_LONG).show();

    }
});
builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialogInterface, int i) {

        Toast.makeText(MainActivity.this, "取消", Toast.LENGTH_LONG).show();
    }
});

builder.show();

列表框

效果

代码

    //条目数组
    final String[] arr = new String[]{
            "香蕉", "苹果", "橘子", "西瓜", "葡萄"
    };

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


    builder.setTitle("请选择你喜欢的水果!");
    //设置条目
    builder.setItems(arr, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, arr[i], Toast.LENGTH_LONG).show();
        }
    });
    //创建对话框并且显示
    builder.create().show();

单选列表框

效果

代码

    //条目数据
    final String[] arr = new String[]{
            "香蕉", "苹果", "橘子", "西瓜", "葡萄"
    };

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


    builder.setTitle("请选择你喜欢的水果!");
    //设置条目
    builder.setSingleChoiceItems(arr, 0, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Toast.makeText(MainActivity.this, arr[i], Toast.LENGTH_LONG).show();

        }
    });


    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });

    builder.setNeutralButton("取消", null);

    builder.create().show();

进度条

效果

代码

        final ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

        //设置标题
        progressDialog.setTitle("正在下载...");

        //设置风格
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        //设置最大大进度
        progressDialog.setMax(100);

        progressDialog.setButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        progressDialog.setButton2("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });


        progressDialog.show();

        //创建线程模拟下载进度
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (i <= 100) {

                    i++;
                    //设置进度
                    progressDialog.incrementProgressBy(i);
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        }).start();

多选列表框

效果

代码

    //条目数据
    final String[] arr = new String[]{
            "香蕉", "苹果", "橘子", "西瓜", "葡萄"
    };

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


    builder.setTitle("请选择你喜欢的水果!");
    //设置条目
    builder.setMultiChoiceItems(arr, new boolean[]{false, false, false, false, false}, new DialogInterface.OnMultiChoiceClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i, boolean b) {

            if (b) {

                Toast.makeText(MainActivity.this, arr[i], Toast.LENGTH_LONG).show();

            }

        }
    });


    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });

    builder.setNeutralButton("取消", null);

    builder.create().show();

自定义消息框

效果

代码

    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    View view1 = View.inflate(MainActivity.this, R.layout.item, null);
    builder.setView(view1);

    EditText et_user = view1.findViewById(R.id.et_user);
    EditText et_pass = view1.findViewById(R.id.et_pass);


    Button btn_login = view1.findViewById(R.id.btn_login);
    Button btn_right = view1.findViewById(R.id.btn_right);


    final AlertDialog alertDialog = builder.create();
    btn_login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_LONG).show();
            alertDialog.dismiss();
        }
    });

    btn_right.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_LONG).show();
            alertDialog.dismiss();
        }
    });
    alertDialog.show();

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="vertical"
    android:padding="10dp">


    <EditText
        android:hint="请输入用户名"
        android:id="@+id/et_user"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <EditText
        android:hint="请输入密码"
        android:id="@+id/et_pass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"

        >


        <Button
            android:id="@+id/btn_login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="登陆" />

        <Button
            android:id="@+id/btn_right"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="注册" />

    </LinearLayout>


</LinearLayout>

读取进度框

效果

代码

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);

progressDialog.setTitle("正在读取");

progressDialog.setIndeterminate(true);

progressDialog.setCancelable(true);

progressDialog.show();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值