当前位置: 首页 > 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.lryc.cn/news/623620.html

相关文章:

  • 飞算JavaAI结合Redis实现高性能存储:从数据瓶颈到极速读写的实战之旅
  • 关于虾的智能养殖系统的开发与实现(LW+源码+讲解+部署)
  • 数据结构(排序篇)——七大排序算法奇幻之旅:从扑克牌到百亿数据的魔法整理术
  • 三维重建-动手学计算机视觉19(完结)
  • SHAP分析!NRBO-Transformer-BiLSTM回归预测SHAP分析,深度学习可解释分析!
  • ReID/OSNet 算法模型量化转换实践
  • 牛客周赛 Round 105
  • Redis-plus-plus API使用指南:通用操作与数据类型接口介绍
  • EDMA(增强型直接内存访问)技术
  • [每周一更]-(第155期):Go 1.25 发布:新特性、技术思考与 Go vs Rust 竞争格局分析
  • 多线程—飞机大战(加入排行榜功能版本)
  • 亚马逊拉美市场爆发:跨境卖家的本土化增长方程式
  • UE5多人MOBA+GAS 48、制作闪现技能
  • 第四章:大模型(LLM)】06.langchain原理-(7)LangChain 输出解析器(Output Parser)
  • CSS中linear-gradient 的用法
  • 【Python】Python 面向对象编程详解​
  • 多线程—飞机大战(加入播放音乐功能版本)
  • macos 安装nodepad++ (教程+安装包+报错后的解决方法)
  • Sentinel和12.5米高程的QGIS 3D效果
  • scikit-learn/sklearn学习|套索回归Lasso解读
  • scikit-learn RandomizedSearchCV 使用方法详解
  • scikit-learn 中的均方误差 (MSE) 和 R² 评分指标
  • .NET 中的延迟初始化:Lazy<T> 与LazyInitializer
  • 『搞笑名称生成器』c++小游戏
  • Spring Cloud整合Eureka、ZooKeeper、原理分析
  • 云计算-k8s实战指南:从 ServiceMesh 服务网格、流量管理、limitrange管理、亲和性、环境变量到RBAC管理全流程
  • 【Kubernetes系列】Kubernetes中的resources
  • 脉冲计数实现
  • vue3 ref和reactive的区别和使用场景
  • Nightingale源码Linux进行跨平台编译