Android studio 之 对话框

普通对话框

用构建器创建对话框:先创建对话框再显示

setTitle 设置对话框标题

setMessage 设置对话框展示消息

setPositiveButton 设置对话框确定按钮

setNegativeButton 设置对话框取消按钮

setlcon() 设置对话框图标

复制代码
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("提示")
       .setMessage("您确定要退出程序吗?")
       .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            })
       .setNegativeButton("取消", null).show();

自定义对话框

创建对话框布局

复制代码
<?xml version="1.0" encoding="utf-8"?>
<!--
    自定义对话框-1.为对话框设置布局
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@mipmap/download1"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="200dp"
        android:text="您确定要退出吗?"
        android:textColor="@color/white"
        android:textSize="34dp"
        android:textStyle="bold"/>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_margin="25dp"
        android:gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/yes_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定"/>
        <Button
            android:id="@+id/no_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="取消"/>
    </LinearLayout>
</LinearLayout>

设置风格:在values下的themes文件中添加以下字段

复制代码
<!--
        自定义对话框-2.设置style
        <item name="android:windowFullscreen">true</item>   要全屏吗? 对
        <item name="android:windowNoTitle">true</item> 不要标题吗? 对
    -->
    <style name="Mydialog" parent="android:style/Theme.Dialog">
        <item name="android:windowNoTitle">true</item>
    </style>

创建对话框布局对应的activity类,将对话框布局应用到当前的自定义dialog中,并在这个类中设置按钮的点击事件

复制代码
package com.example.androidstudiostudy;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import androidx.annotation.NonNull;

// 创建一个自定义对话框类
public class MyDialog extends Dialog {
    // 只有一个参数的构造方法。传递需要添加自定义对话框的环境上下文
    /*public MyDialog(@NonNull Context context) {
        super(context);
        // 1.为对话框设置布局
        setContentView(R.layout.dialog_layout);
    }*/
    // 有两个参数的构造方法
    // 参数1:需要添加自定义对话框的环境上下文
    // 参数2: 设置的样式id
    public MyDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        // 自定义对话框-3.将布局应用到当前自定义的对话框
        setContentView(R.layout.dialog_layout);

        // 为对话框的两个按钮设置点击方法
        findViewById(R.id.yes_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击确认 -退出
                System.exit(0);
            }
        });
        findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 点击取消 -取消弹窗
                dismiss();
            }
        });
    }
}

.实例化对话框,根据对话框类的构造函数不同传入不同的参数,此时传入的是 环境上下文和样式id,并显示.show()

复制代码
/ 自定义对话框-4.实例化对话框并显示
传入当前环境变量和第2步设置好的风格样式id*/
MyDialog myDialog = new MyDialog(this,R.style.Mydialog);
myDialog.show();
相关推荐
在努力的前端小白3 小时前
Spring Boot 敏感词过滤组件实现:基于DFA算法的高效敏感词检测与替换
java·数据库·spring boot·文本处理·敏感词过滤·dfa算法·组件开发
专注API从业者4 小时前
Python + 淘宝 API 开发:自动化采集商品数据的完整流程
大数据·运维·前端·数据挖掘·自动化
烛阴5 小时前
TypeScript高手密技:解密类型断言、非空断言与 `const` 断言
前端·javascript·typescript
一叶飘零_sweeeet5 小时前
从繁琐到优雅:Java Lambda 表达式全解析与实战指南
java·lambda·java8
艾伦~耶格尔6 小时前
【集合框架LinkedList底层添加元素机制】
java·开发语言·学习·面试
样子20186 小时前
Uniapp 之renderjs解决swiper+多个video卡顿问题
前端·javascript·css·uni-app·html
Nicholas686 小时前
flutterAppBar之SystemUiOverlayStyle源码解析(一)
前端
一只叫煤球的猫6 小时前
🕰 一个案例带你彻底搞懂延迟双删
java·后端·面试
最初的↘那颗心6 小时前
Flink Stream API 源码走读 - print()
java·大数据·hadoop·flink·实时计算
黑客飓风6 小时前
JavaScript 性能优化实战大纲
前端·javascript·性能优化