Android Basic Learning Day06 | Dialog

Words written in the front

1. The content is referenced from the relevant Android videos and internet at station B.
2. If there is something wrong with the content, I hope you can point it out or add it.
3. New knowledge.
4. Mainly to test the usage, and then add and perfect it.

I. Overview

Dialog: It is also a way for the program to interact with the user. It is usually used to display the current program prompt information and related instructions, displayed in a small window (pop-up).

Mainly divided into the following categories:

  • AlertDialog: Warning dialog (prompt dialog).
  • ProgressDialog: Progress dialog box.
  • DatePickerDialog: date selection dialog box.
  • TimerPickerDialog: Time selection dialog box.
  • Customize the dialog box.

Two, common dialog

The dialog box can be basically divided into three parts, title, content, and confirm cancel button.

They are all tested in the MainActivity.java file.

(1) Prompt dialog box

1. Common dialog box

① Write the code as follows.

Note: setPositiveButton is generally used for confirm button, setNegativeButton is generally used for cancel button.

        //普通对话框
        AlertDialog dialog = new AlertDialog.Builder(this)
                //弹出框的标题
                .setTitle("这是一个普通对话框")
                //提示的内容信息
                .setMessage("是否退出")
                //标题的图标
                .setIcon(R.mipmap.ic_launcher)

                //添加确认、取消按钮+提示信息
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
    
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
    
                        //提示信息
                        Toast.makeText(MainActivity.this, "已退出",
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("否", new DialogInterface.OnClickListener() {
    
    
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
    
    
                        Toast.makeText(MainActivity.this, "已取消",
                                Toast.LENGTH_SHORT).show();
                    }
                })
                .create();//确定创建

        dialog.show();//显示对话框

② The effect is as follows.
Insert picture description here
2. Single selection dialog

这里的内容部分就是单选按钮了,所以不可以再设置setMessage,否则会将选项给覆盖掉。
① Write the code as follows.

        //单选对话框
        AlertDialog dialog2 = new AlertDialog.Builder(this)
                .setTitle("请选择您的性别")
                .setIcon(R.mipmap.ic_launcher)
                .setPositiveButton("确定",null)
                //checkedItem值为0则是默认选中第一个按钮
                .setSingleChoiceItems(new String[]{
    
    "男", "女"}, -1,null)
                .create();
        dialog2.show();

② The effect is as follows.
Insert picture description here
3. Multiple selection dialog

① Write the code as follows.

        //多选对话框
        AlertDialog dialog3 = new AlertDialog.Builder(this)
                .setTitle("请选择您喜欢的猫咪品种")
                .setIcon(R.mipmap.title)
                .setPositiveButton("确定",null)

                .setMultiChoiceItems(new String[]{
    
    "布偶猫", "缅因猫", "波斯猫", "挪威森林猫"},null, null)
                .create();
        dialog3.show();

② The effect is as follows.
Insert picture description here

(2) Progress dialog

① Write the code as follows.

        //进度条对话框
        ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setTitle("这是一个进度条对话框");
        progressDialog.setIcon(R.mipmap.ic_launcher);
        progressDialog.setMessage("正在加载猫咪资源中,请稍等");
        //进度条的样式(系统提供) 默认是个圆圈
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.show();

② The effect is as follows.
Insert picture description here

Guess you like

Origin blog.csdn.net/luck_ch09/article/details/112515755