当前位置: 首页 > news >正文

Android 对话框 - 基础对话框补充(不同的上下文创建 AlertDialog、AlertDialog 的三个按钮)

一、不同的上下文创建 AlertDialog

1、演示
(1)使用 Activity 上下文
AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("普通对话框");
builder.setMessage("确定退出吗?");
builder.setPositiveButton("确定", (dialog, which) -> {Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) -> {Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
});AlertDialog alertDialog = builder.create();alertDialog.show();
(2)使用 Application 上下文
  1. 使用 getApplication 方法获取的 Application 上下文
AlertDialog.Builder builder = new AlertDialog.Builder(getApplication());builder.setTitle("普通对话框");
builder.setMessage("确定退出吗?");
builder.setPositiveButton("确定", (dialog, which) -> {Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) -> {Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
});AlertDialog alertDialog = builder.create();alertDialog.show();
  • 输出结果
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
  1. 使用 getApplicationContext 方法获取的 Application 上下文
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());builder.setTitle("普通对话框");
builder.setMessage("确定退出吗?");
builder.setPositiveButton("确定", (dialog, which) -> {Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) -> {Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
});AlertDialog alertDialog = builder.create();alertDialog.show();
  • 输出结果
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
2、小结
  1. 应该总是使用 Activity 上下文来创建对话框

  2. 避免使用 Application 上下文来创建对话框,对话框需要依附于一个窗口,而 Application 上下文没有关联的窗口


二、AlertDialog 的三个按钮

1、基本介绍
按钮说明
Positive Button积极 / 确定按钮,通常表示确认或继续操作
Negative Button消极 / 取消按钮,通常表示取消操作
Neutral Button中性按钮,提供第三种选择
2、演示
AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setTitle("普通对话框");
builder.setMessage("确定退出吗?");
builder.setPositiveButton("确定", (dialog, which) -> {Toast.makeText(this, "点击了确定", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("取消", (dialog, which) -> {Toast.makeText(this, "点击了取消", Toast.LENGTH_SHORT).show();
});
builder.setNeutralButton("稍后", (dialog, which) -> {Toast.makeText(this, "点击了稍后", Toast.LENGTH_SHORT).show();
});AlertDialog alertDialog = builder.create();alertDialog.show();
http://www.dtcms.com/a/335688.html

相关文章:

  • Pandas数据结构详解Series与DataFrame
  • 智能汽车领域研发,复用云原始开发范式?
  • 笔记本电脑wifi小图标不见了 或者 蓝牙功能消失、电脑开不开机解决方法
  • 深入理解C#特性:从应用到自定义
  • CentOS启动两个MySQL实例
  • C#对象的本地保存与序列化详解笔记
  • [每周一更]-(第155期):Go 1.25 发布:新特性、技术思考与 Go vs Rust 竞争格局分析
  • 【前端面试题】前端面试知识点(第三十一题到第六十一题)
  • 项目发布上线清单
  • neo4j导入导出方法
  • 《设计模式》抽象工厂模式
  • 链表OJ题讲解---试金石含金量
  • RabbitMQ入门:生产者和消费者示例
  • Java注解学习记录
  • 什么是EDA(Exploratory Data Analysis,探索性数据分析)
  • AI出题人给出的Java后端面经(十七)(日更)
  • 第 463 场周赛(GPT-3,Me-1)
  • Foreign-Memory Access API外部内存API
  • 混沌工程(Chaos engineering):系统韧性保障之道
  • 计算机网络 HTTPS 全流程
  • p5.js 3D 形状 “预制工厂“——buildGeometry ()
  • 【位运算】查询子数组最大异或值|2693
  • 图灵完备(Turing Complete)免安装中文版
  • 关于pygsp引发的一系列问题和实例小demo
  • ​​Vue 3 开发速成手册
  • 裸机框架:按键模组
  • macos 安装nodepad++ (教程+安装包+报错后的解决方法)
  • AI证书怎么选
  • 交叉编译 手动安装 SQLite 库 移植ARM
  • 基于Vue + Node能源采购系统的设计与实现/基于express的能源管理系统#node.js