Android 常见对话框的简单使用(提示信息对话框、单选多选对话框、自定义对话框)

简介: Android 常见对话框的简单使用(提示信息对话框、单选多选对话框、自定义对话框)

一、提示信息对话框:

0.png

//显示提示消息对话框
    private void showMsgDialog() {
        //创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框标题
        builder.setTitle("提示信息对话框");
        //设置提示信息
        builder.setMessage("是否确定退出!");
        //设置对话框图标
        builder.setIcon(R.mipmap.ic_launcher);
        //添加确定按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //添加确定按钮点击的处理代码
                Toast.makeText(MainActivity.this, "点击了确定!", Toast.LENGTH_SHORT).show();
            }
        });
        //添加取消按钮
        builder.setNegativeButton("取消",null);
        //创建并显示对话框
        builder.show();
    }

二、单选对话框:


1.png

    //显示单选对话框
    private void showSingleChoiceDialog() {
        //创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框标题
        builder.setTitle("请选择性别");
        //设置对话框图标
        builder.setIcon(R.mipmap.ic_launcher);
        final String[] sexs = new String[]{"男", "女"};
        //设置单选选项
        builder.setSingleChoiceItems(sexs, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "您选择了:"+sexs[which], Toast.LENGTH_SHORT).show();
            }
        });
        //添加确定按钮
        builder.setPositiveButton("确定", null);
        //创建并显示对话框
        builder.show();
    }


三、多选对话框:


2.png

 //显示多选对话框
    private void showMultiChoiceDialog() {
        //创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框标题
        builder.setTitle("请选择传感器");
        //设置对话框图标
        builder.setIcon(R.mipmap.ic_launcher);
        final String[] sensors = new String[]{"温湿度传感器", "光照传感器","CO2传感器","风速传感器"};
        //设置多选选项
        builder.setMultiChoiceItems(sensors, new boolean[]{false,true,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            }
        });
        //添加确定按钮
        builder.setPositiveButton("确定", null);
        //创建并显示对话框
        builder.show();
    }


四、自定义对话框:

自定义对话框布局:


3.png

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#2372c1"
        android:gravity="center"
        android:text="提示"
        android:padding="5dp"
        android:textColor="#fff"
        android:textSize="25sp" />
    <TextView
        android:id="@+id/tvContent"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center"
        android:text="自定义对话框内容" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#c0c0c0"
        android:gravity="center"
        android:orientation="horizontal">
        <Button
            android:id="@+id/btnOk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定" />
        <Button
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:text="取消" />
    </LinearLayout>
</LinearLayout>
MyDialog.Java


package com.newland.dialogdemo;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MyDialog extends Dialog {
    private String title;
    private String content;
    private TextView tvTitle;
    private TextView tvContent;
    private Button btnOk;
    private Button btnCancel;
    public MyDialog(Context context) {
        super(context);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //引入自定义对话框布局
        setContentView(R.layout.my_dialog);
        //初始化控件
        initView();
        //设置标题
        tvTitle.setText(title);
        //设置内容
        tvContent.setText(content);
        //注册确认按钮监听器
        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击确认时的操作
            }
        });
        //注册取消按钮监听器
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭对话框
                dismiss();
            }
        });
    }
    //初始化控件
    private void initView() {
        tvTitle = findViewById(R.id.tvTitle);
        tvContent = findViewById(R.id.tvContent);
        btnOk = findViewById(R.id.btnOk);
        btnCancel = findViewById(R.id.btnCancel);
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public void setContent(String content) {
        this.content = content;
    }
}
MainActivity.Java


    //显示自定义对话框
    private void showCustomDialog() {
        MyDialog dialog = new MyDialog(this);
        dialog.setTitle("自定义对话框");
        dialog.setContent("你好!这里是自定义对话框!");
        dialog.show();
    }


演示项目完整代码:


4.png

<?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:orientation="vertical">
    <Button
        android:id="@+id/btnShowMsgDlg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提示信息对话框"/>
    <Button
        android:id="@+id/btnShowSingleDlg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="单选对话框"/>
    <Button
        android:id="@+id/btnShowMultiDlg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="多选对话框"/>
    <Button
        android:id="@+id/btnShowCustomDlg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="自定义对话框"/>
</LinearLayout>


package com.newland.dialogdemo;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    //显示提示信息对话框按钮
    private Button btnShowMsgDlg;
    //显示单选对话框按钮
    private Button btnShowSingleDlg;
    //显示多选对话框按钮
    private Button btnShowMultiDlg;
    //显示自定义对话框按钮
    private Button btnShowCustomDlg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化视图以及监听器
        initView();
    }
    //初始化视图以及监听器
    private void initView() {
        //初始化控件
        btnShowMsgDlg = findViewById(R.id.btnShowMsgDlg);
        btnShowSingleDlg = findViewById(R.id.btnShowSingleDlg);
        btnShowMultiDlg = findViewById(R.id.btnShowMultiDlg);
        btnShowCustomDlg = findViewById(R.id.btnShowCustomDlg);
        //注册按钮监听器
        btnShowMsgDlg.setOnClickListener(this);
        btnShowSingleDlg.setOnClickListener(this);
        btnShowMultiDlg.setOnClickListener(this);
        btnShowCustomDlg.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnShowMsgDlg: //显示提示信息对话框按钮
                showMsgDialog();
                break;
            case R.id.btnShowSingleDlg: //显示单选对话框按钮
                showSingleChoiceDialog();
                break;
            case R.id.btnShowMultiDlg:  //显示多选对话框按钮
                showMultiChoiceDialog();
                break;
            case R.id.btnShowCustomDlg: //显示自定义对话框按钮
                showCustomDialog();
                break;
        }
    }
    //显示提示消息对话框
    private void showMsgDialog() {
        //创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框标题
        builder.setTitle("提示信息对话框");
        //设置提示信息
        builder.setMessage("是否确定退出!");
        //设置对话框图标
        builder.setIcon(R.mipmap.ic_launcher);
        //添加确定按钮
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //添加确定按钮点击的处理代码
                Toast.makeText(MainActivity.this, "点击了确定!", Toast.LENGTH_SHORT).show();
            }
        });
        //添加取消按钮
        builder.setNegativeButton("取消",null);
        //创建并显示对话框
        builder.show();
    }
    //显示单选对话框
    private void showSingleChoiceDialog() {
        //创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框标题
        builder.setTitle("请选择性别");
        //设置对话框图标
        builder.setIcon(R.mipmap.ic_launcher);
        final String[] sexs = new String[]{"男", "女"};
        //设置单选选项
        builder.setSingleChoiceItems(sexs, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "您选择了:"+sexs[which], Toast.LENGTH_SHORT).show();
            }
        });
        //添加确定按钮
        builder.setPositiveButton("确定", null);
        //创建并显示对话框
        builder.show();
    }
    //显示多选对话框
    private void showMultiChoiceDialog() {
        //创建AlertDialog构造器Builder对象,AlertDialog建议使用android.support.v7.app包下的。
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //设置对话框标题
        builder.setTitle("请选择传感器");
        //设置对话框图标
        builder.setIcon(R.mipmap.ic_launcher);
        final String[] sensors = new String[]{"温湿度传感器", "光照传感器","CO2传感器","风速传感器"};
        //设置多选选项
        builder.setMultiChoiceItems(sensors, new boolean[]{false,true,true,false}, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
            }
        });
        //添加确定按钮
        builder.setPositiveButton("确定", null);
        //创建并显示对话框
        builder.show();
    }
    //显示自定义对话框
    private void showCustomDialog() {
        MyDialog dialog = new MyDialog(this);
        dialog.setTitle("自定义对话框");
        dialog.setContent("你好!这里是自定义对话框!");
        dialog.show();
    }
}
相关文章
|
23天前
|
XML IDE 开发工具
【Android UI】自定义带按钮的标题栏
【Android UI】自定义带按钮的标题栏
30 7
【Android UI】自定义带按钮的标题栏
|
9天前
|
搜索推荐 数据库 Android开发
自定义头像 Android
【6月更文挑战第16天】
|
23天前
|
XML API Android开发
android上FragmentTabHost实现自定义Tab Indicator
android上FragmentTabHost实现自定义Tab Indicator
18 1
|
23天前
|
XML 前端开发 API
Android中实现Bitmap在自定义View中的放大与拖动
Android中实现Bitmap在自定义View中的放大与拖动
59 1
|
1天前
|
Android开发
Android自定义View之正方形
【6月更文挑战第23天】
|
2天前
|
搜索推荐 Android开发 开发者
Android 自定义组件
Android 自定义组件
6 0
|
27天前
|
API Android开发
32. 【Android教程】对话框:AlertDialog
32. 【Android教程】对话框:AlertDialog
21 2
|
9天前
|
开发工具 Android开发
Android 代码自定义drawble文件实现View圆角背景
Android 代码自定义drawble文件实现View圆角背景
15 0
|
9天前
|
Android开发
Android 自定义View 测量控件宽高、自定义viewgroup测量
Android 自定义View 测量控件宽高、自定义viewgroup测量
12 0
|
9天前
|
开发工具 Android开发 git
Android自定义View——可以设置最大宽高的FrameLayout
Android自定义View——可以设置最大宽高的FrameLayout
20 0