• 【Android】UI组件之AlertDialog


    1、对话框定义

    对话框是程序运行中的弹出窗口,例如,当用户要删除一个联系方式时,会弹出一个对话框,让用户确认是否真的要删除。

    Android系统提供了多种对话框:

    • AlertDialog(警告对话框)
    • ProgressDialog(进度条对话框)
    • DatePickerDialog(日期选择对话框)
    • TimerPickerDialog(时间选择对话框)

    2、AlertDialog创建方式

    • title 标题是可选部分
    • Content Area 显示message、list…
    • Action Buttons 可以添加操作,不超过三个操作按钮
    package com.example.test_alertdialog;
    import androidx.appcompat.app.AlertDialog;
    import androidx.appcompat.app.AppCompatActivity;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void test1(View view){
            //如何创建一个alertdialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息!");
            builder.setMessage("我的第一个对话框!");
    
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:onClick="test1"
            tools:layout_editor_absoluteX="44dp"
            tools:layout_editor_absoluteY="52dp"
            />
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    请添加图片描述

    3、对话框添加按钮

    包括不同性质的按钮:setPositiveButton、setNegativeButton、setNeutralButton

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void test1(View view){
            //如何创建一个alertdialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息!");
            builder.setMessage("我的第一个对话框!");
    
            builder.setPositiveButton("非常满意", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(MainActivity.this, "非常满意", Toast.LENGTH_SHORT).show();
                }
            });
    
    
            builder.setNegativeButton("不满意", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(MainActivity.this, "不满意", Toast.LENGTH_SHORT).show();
                }
            });
    
            builder.setNeutralButton("一般", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(MainActivity.this, "一般", Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    请添加图片描述
    请添加图片描述

    4、对话框点击显示

    public void test2(View view){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息!");
            builder.setItems(ss, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
     <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button2"
            android:layout_below="@id/button1"
            android:onClick="test2"
            />
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    请添加图片描述

    5、对话框单项选择

    解析:

    builder.setSingleChoiceItems(ss, **0**, new DialogInterface.OnClickListener(){}
    //其中的0,表示初始点开时选中数组中第0个
    
    • 1
    • 2

    代码:

    public void test3(View view){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息!");
    
            builder.setSingleChoiceItems(ss, 0, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
     <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button3"
            android:layout_below="@id/button2"
            android:onClick="test3"
            />
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    请添加图片描述

    6、对话框多项选择

    解析:

    builder.setMultiChoiceItems(ss, null, new DialogInterface.OnMultiChoiceClickListener() {}
     //其中的null,表示初始点开时不选中数组中的任意值
     //null也可换成new boolean[]{true,false,true},true为选中,false为不选中
    
    • 1
    • 2
    • 3

    代码:

    public void test4(View view){
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息!");
    
            builder.setMultiChoiceItems(ss, null, new DialogInterface.OnMultiChoiceClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i, boolean b) {
                    Toast.makeText(MainActivity.this, ss[i], Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button4"
            android:onClick="test4"
            android:layout_below="@id/button3"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    请添加图片描述

  • 相关阅读:
    多方合作时,系统间的交互是怎么做的?
    华为OD机试之最小调整顺序次数、特异性双端队列(Java源码)
    MySQL 读写分离配置实践
    Java学习笔记(三十二)
    【微软技术栈】C#.NET 元组类型
    最简单的共享列表服务器KissLists
    datax使用笔记
    Java基础教程:dubbo源码解析-网络通信
    弘玑RPA进阶攻略
    13-ES5和ES6基础
  • 原文地址:https://blog.csdn.net/weixin_51293984/article/details/126245037