AlertDialog对话框的简单使用
目录
一、对话框的创建
二、单选的对话框
三、多选的对话框
一、对话框的创建
一般的对话框分为标题、内容、按钮三大部分。
常见的方法:
方法 | 功能 |
setTitle() | 设置对话框的标题 |
setIcon() | 设置对话框的图标 |
setMessage() | 设置对话框的提示信息 |
setPositiveButton() | 设置对话框的确认按钮 |
setNegativeButton() | 设置对话框的取消按钮 |
setSingleChoiceItems() | 设置单选的对话框 |
setMultiChoiceItems() | 设置多选的对话框 |
dismiss() | 关闭对话框 |
DialogInterface.OnClickListener() | 设置对话框的点击事件 |
对话框创建步骤:
1、声明AlertDialong对象
2、调用AlertDialog的内部类Builder,设置相关属性。
3、创建对话框 ( dialog = builder.create() )
4、显示对话框( dialog.show() )
代码如下:
package com.example.alertdialog;import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;import android.content.DialogInterface;
import android.os.Bundle;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}// 当想要退出APP时,调用该方法@Overridepublic void onBackPressed() {// 声明对象AlertDialog dialog;AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("普通的对话框").setIcon(R.mipmap.ic_launcher).setMessage("您确定要退出吗?").setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss(); // 关闭对话框MainActivity.this.finish(); // 退出APP}})// 添加取消按钮.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 点击取消,对话框消失dialog.dismiss();}});// 创建对话框dialog = builder.create();// 显示对话框dialog.show();}
}
效果:
二、单选的对话框
案例:通过单选对话框改变字体的颜色
效果图:
MainActivity:
package com.example.alertdialog_single_choice;import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {TextView mTextView;Button btn;// 颜色数组Integer []textColors = {android.R.color.holo_green_light,R.color.black, android.R.color.holo_blue_dark, android.R.color.holo_red_dark};// 数组索引int index = 1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btn = findViewById(R.id.btn);mTextView = findViewById(R.id.content);btn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {AlertDialog dialog;AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this).setTitle("设置字体颜色").setIcon(R.mipmap.ic_launcher)// 设置单选的内容.setSingleChoiceItems(new String[]{"绿色","黑色", "蓝色", "红色"}, index, new DialogInterface.OnClickListener() {// which 就是选到那个item的位置public void onClick(DialogInterface dialog, int which) {// 获取数组的索引index = which;}})// 确认按钮.setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// 确认就设置字体的颜色mTextView.setTextColor(getResources().getColor(textColors[index]));dialog.dismiss();}})// 取消按钮.setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});// 创建dialog对象dialog = builder.create();// 显示对话框dialog.show();}});}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:id="@+id/content"android:layout_gravity="center"android:textSize="30sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!" /><Buttonandroid:id="@+id/btn"android:layout_gravity="center"android:textSize="15sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="点击按钮选择字体颜色"/></LinearLayout>
三、多选的对话框
案例:通过多选对话框选择自己的兴趣爱好,并显示到文本框。
效果:
MainActivity:
package com.example.alertdialog_multiple_choice;import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {private TextView mTextView;private Button mButton;private String []hobbies ={"看电影","旅游","运动","听音乐"};private boolean []checked = {false,false,false,false};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mTextView = findViewById(R.id.text_view);mButton = findViewById(R.id.btn);mButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {AlertDialog dialog;AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this).setTitle("请选择你的兴趣").setIcon(R.mipmap.ic_launcher).setMultiChoiceItems(hobbies, checked, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {checked[which] = isChecked;}}).setPositiveButton("确认", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {StringBuilder stringBuilder = new StringBuilder();for (int i = 0; i < checked.length; i++) {if (checked[i]){stringBuilder.append(hobbies[i]).append(" ");}if (stringBuilder!=null){mTextView.setText(stringBuilder);}}dialog.dismiss();}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {dialog.dismiss();}});dialog = builder.create();dialog.show();}});}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:orientation="vertical"android:layout_height="match_parent"><TextViewandroid:textSize="18sp"android:id="@+id/text_view"android:layout_width="match_parent"android:layout_height="70dp"/><Buttonandroid:id="@+id/btn"android:layout_gravity="center"android:text="请选择你的兴趣"android:layout_width="wrap_content"android:layout_height="wrap_content"/></LinearLayout>
AlertDialog对话框的简单使用相关推荐
- AlertDialog对话框的使用及示例
AlertDialog对话框用于提示一些重要信息或者显示一些需要用户额外交互的内容.它一般以小窗口的形式展示在界面上.使用AlertDialog创建的对话框一般包含标题.内容和按钮三个区域. 创建Al ...
- AlertDialog对话框的使用
文章目录 创建AlertDialog对话框的步骤 普通对话框 单选对话框 多选对话框 自定义对话框 在Android程序中,AlertDialog对话框用于提示一些重要信息或者显示一些需要用户额外交互 ...
- Android基础入门教程——2.5.3 AlertDialog(对话框)详解
Android基础入门教程--2.5.3 AlertDialog(对话框)详解 标签(空格分隔): Android基础入门教程 本节引言: 本节继续给大家带来是显示提示信息的第三个控件AlertDia ...
- 【Android 常见控件使用】AlertDialog(对话框)详解
文章目录 AlertDialog(对话框)详解 本节引言 1.基本使用流程 2.几种常用的对话框使用示例 3.通过Builder的setView()定制显示的AlertDialog AlertDial ...
- Android AlertDialog 对话框开启和关闭输入法问题
Android AlertDialog 对话框开启和关闭输入法问题 AlertDialog 对话框是android jar包自带的对话框,说实话 真的很丑,黑白相间,不是很推荐使用.大多时候 特别是项 ...
- 一对一视频聊天软件源码,Android AlertDialog 对话框
一对一视频聊天软件源码,Android AlertDialog 对话框实现的相关代码 button = (Button) findViewById(R.id.button_alertDialog);b ...
- AlertDialog对话框详例
Android-3分钟你也能学会AlertDialog对话框 在Android开发中,少不了AlertDialog对话框的使用,也是至关重要的一个功能.AlertDialog的构造方法被声明为prot ...
- Android App开发超实用实例 | AlertDialog对话框
介绍AlertDialog对话框从简到繁的多种设计方案,从新角度理解UI交互设计. AlertDialog对话框允许自定义弹出对话框的内容,实现比之前讲解的几种对话框更丰富的功能. 01.带默认按钮的 ...
- Android AlertDialog对话框不能阻塞进程
因为AlertDialog对话框不能阻塞当前进程,如果在对话框后,有继续运行的代码,或有切换界面的代码,对话框会一闪而过,不等用户选择. 正确的做法应该是: if (条件){ AlertDialog ...
最新文章
- 二叉树-二叉树的镜像(递归法)
- python绘制k线图的步骤_Python使用PyQtGraph绘制股票行情K线图
- 华为交换机的配置及:access、trunk、hybird端口详解
- Android Chart框架 MPAndroidChart 坐标轴设置
- CSDN排名及积分规则
- dotnetCore增加MiddleWare的Run,Use Map MapThen四个扩展方法
- 9203 演练 jsp实现增删改查
- CentOS 5.8 Zimbra邮件系统安装与配置
- 机器学习与计算机视觉(绘图)
- 【oracle11g,17】存储结构: 段的类型,数据块(行连接、行迁移,块头),段的管理方式,高水位线...
- KMP模式匹配的next数组
- windows 界面设计规则与规范
- 为什么 PSP22 对 Polkadot 生态系统很重要
- 【车联网原型系统|三】树莓派设计+模拟基站程序
- 【第2重磅】王者荣耀「绝悟」升级,全英雄池解禁
- Rockchip RK3588 kernel dts解析之系统休眠配置rockchip_suspend
- java tls_Java客户端中具有SNI的TLS
- 程序员学习指南_程序员管理压力指南
- OmniPlayer Pro for Mac v1.4.12 支持无线投屏的视频播放器
- tomcat宕机自动重启和每日定时启动tomcat