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

android13打基础: 控件alertdialog

测试alertdialog的activity

// todo: 高级控件alertdialog
public class Ch4_AlertDialogActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alertdialog_ch4);

        findViewById(R.id.ch4_btn_alert).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ch4_btn_alert) {
            // 创建提醒对话框的Builder
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            // 对话框标题文本
            builder.setTitle("尊敬的用户");
            // 对话框的内容文本
            builder.setMessage("你真的要卸载我吗?");
            builder.setPositiveButton("残忍卸载", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    TextView tv_alert = findViewById(R.id.ch4_tv_alert);
                    tv_alert.setText("删除了");
                }
            });

            builder.setNegativeButton("不卸载", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    TextView tv_alert = findViewById(R.id.ch4_tv_alert);
                    tv_alert.setText("删除失败");
                }
            });

            AlertDialog alertDialog = builder.create();
            alertDialog.show();
        }
    }
}

布局文件

<?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/ch4_btn_alert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="弹出提醒对话框"
        android:textColor="@color/black"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/ch4_tv_alert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:textColor="@color/black"
        android:textSize="17sp" />

</LinearLayout>

点击按钮,弹出对话框


http://www.kler.cn/a/572150.html

相关文章:

  • 基于javaweb的SSM+Maven教务管理系统设计和实现(源码+文档+部署讲解)
  • 关于签名验证不存在的错误
  • Docker 学习(二)——基于Registry、Harbor搭建私有仓库
  • Android14 串口控制是能wifi adb实现简介
  • 高频 SQL 50 题(基础版)_550. 游戏玩法分析 IV
  • 详解 scanf 和 printf(占位符、printf、scanf的返回值、printf的输出格式、scanf的输入格式)
  • 费曼学习法13 - 数据表格的魔法:Python Pandas DataFrame 详解 (Pandas 基础篇)
  • Iceberg Catalog
  • iOS安全和逆向系列教程 第3篇:搭建iOS逆向开发环境 (上) - 工具链与基础配置
  • 软件测试人员在工作中如何运用Linux?
  • 机器视觉线阵相机分时频闪选型/机器视觉线阵相机分时频闪选型
  • Vue 3 组件库文档化最佳实践:Storybook 自动化文档生成 - 打造专业级组件文档
  • 免费轻巧多功能 PDF 处理工具:转换、压缩、提取一应俱全
  • JDBC连接数据库(MySQL)教程(包含可能出错的问题)
  • 迷你世界脚本生物接口:Creature
  • 13天 -- Redis 中如何实现分布式锁? Redis 的 Red Lock 是什么?你了解吗? Redis 实现分布式锁时可能遇到的问题有哪些?
  • c语言(循环(for,while,do while))
  • docker compose 搭建 redis 主从复制集
  • 61. Three.js案例-彩色旋转立方体创建与材质应用
  • 解锁数据抓取新高度:Python 分布式爬虫与逆向进阶实战课