在之前的文章中介绍了自定Dialog的实现方法之一:https://blog.csdn.net/m0_57487205/article/details/124775019?spm=1001.2014.3001.5501https://blog.csdn.net/m0_57487205/article/details/124775019?spm=1001.2014.3001.5501      这篇文章记录一下另外一种实现自定义Dialog的方法:

首先创建一个自己的MyDialog类集成Dialog,集成Dialog 之后会爆红,只需要按Alt+insert键快捷插入构造方法即可,然后需要重写onCreate()方法,在onCreate()方法中通过setContentView(R.layout.dialog_layout)方法指定弹窗需要显示的layout布局,布局可以随便自定义:

1、MyDialog代码:

public class MyDialog extends Dialog {public MyDialog(@NonNull Context context, int themeResId) {super(context, themeResId);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.dialog_layout);}}

2:dialog_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="300dp"android:layout_height="200dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"android:background="@drawable/dialog_shape"app:layout_constraintTop_toTopOf="parent"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:text="温馨提示"android:textColor="#8276F1"android:textSize="20sp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="16dp"android:text="是否确定删除此文件?"android:textSize="15sp"android:textColor="#ff0000"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="@+id/textView" /><Buttonandroid:id="@+id/no"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="16dp"android:layout_marginLeft="16dp"android:layout_marginBottom="16dp"android:text="取消"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent" /><Buttonandroid:id="@+id/yes"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginEnd="16dp"android:layout_marginRight="16dp"android:layout_marginBottom="16dp"android:text="确定"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout></androidx.constraintlayout.widget.ConstraintLayout>

编写好布局文件后就可以对一些逻辑进行整理了,类似于Activity,在onCreate()方法中就可以执行findViewById()操作了,接着就是弹窗按钮的点击时间监听,实现链式编程,一定程度上简化了弹窗的调用流程,下面是完整的MyDialog代码:

/*** 自定义dialog*/
public class MyDialog extends Dialog {private Button button_no;private Button button_yes;private String title = "title";private String message = "message";private DialogClickListener listener;/*** 构造器* @param context 上下文对象* @param themeResId 弹窗样式*/public MyDialog(@NonNull Context context, int themeResId) {super(context, themeResId);}/*** 设置弹窗的标题* @param title 标题内容* @return 返回当前弹窗对象实现链式编程*/public MyDialog setTitle(String title) {this.title = title;return this;}/*** 设置弹窗的提示内容* @param message 内容* @return ***/public MyDialog setMessage(String message) {this.message = message;return this;}/*** 设置点击事件监听对象(传入调用者创建的监听对象)* @param listener 监听对象* @return ***/public MyDialog setClickListener(DialogClickListener listener) {this.listener = listener;return this;}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.dialog_layout);button_no = findViewById(R.id.no);button_yes = findViewById(R.id.yes);TextView textView_title = findViewById(R.id.textView);TextView textView_message = findViewById(R.id.message);textView_title.setText(title);textView_message.setText(message);initListener();}private void initListener() {button_no.setOnClickListener(view -> {if (null != listener)listener.onNO();//接口回调dismiss();});button_yes.setOnClickListener(view -> {if (null != listener)listener.onYes();//接口回调dismiss();});}public interface DialogClickListener {void onYes();void onNO();}}

由于在使用弹窗的时候,调用者需要监听用户到底点了弹窗中的哪一些按钮,或者执行了什么操作,所以需要一个弹窗的接口类进行时间的监听(代码已详细说明,比较好).

最后就是弹窗的调用了:

 private void showDialog() {MyDialog myDialog = new MyDialog(this, R.style.dialog);myDialog.setCancelable(false);//返回键不可以关闭弹窗myDialog.setCanceledOnTouchOutside(false);//点击弹窗之外不能关闭弹窗myDialog.setTitle("温馨提示").setMessage("确定要删除这个文件吗?").setClickListener(new MyDialog.DialogClickListener() {@Overridepublic void onYes() {Log.e("Dialog", "点击确定要做的事情在这里写");}@Overridepublic void onNO() {Log.e("Dialog", "点击取消要做的事情在这里写");}}).show();}

这是效果图)

Android 自定义Dialog实现(二)相关推荐

  1. android自定义dialog对话框,android的自定义dialog对话框实现

    Android自定义dialog对话框 一:自定义dialog 如何填充布局 ①自定MyDialog类,在类里关联布局 public class MyDialog extends AlertDialo ...

  2. android 自定义透明 等待 dialog,Android自定义Dialog内部透明、外部遮罩效果

    Android自定义Dialog内部透明.外部遮罩效果 发布时间:2020-09-09 03:01:41 来源:脚本之家 阅读:117 作者:zst1303939801 本文实例为大家分享了Andro ...

  3. 精通Android自定义View(十二)绘制圆形进度条

    1 绘图基础简析 1 精通Android自定义View(一)View的绘制流程简述 2 精通Android自定义View(二)View绘制三部曲 3 精通Android自定义View(三)View绘制 ...

  4. android dialog 消失动画,android 自定义dialog弹出和消失缩放动画

    本文转自:android 自定义dialog,窗口动画 Java代码: package com.sunxu.org.IndividualityDialog; import Android.app.Ac ...

  5. Android自定义动画专题二

    android自定义动画专题二 在上篇文章中给大家介绍了android自定义动画的第一种表现形式:view的绘制:不过这只是一种单纯利用自定义控件绘制的方式去实现:这篇文章会给大家演示如何通过自定义控 ...

  6. Android自定义拍照上传界面,Android自定义dialog——设置头像(拍照,相册)

    Android自定义dialog--设置头像(拍照,相册) 需求场景:个人信息设置,点击头像,在界面上弹出一个弹框,用户选择"拍照"/"从图库选择",选择照片后 ...

  7. android如何自定义dialog,Android—自定义Dialog

    在 Android 日常的开发中,Dialog 使用是比较广泛的.无论是提示一个提示语,还是确认信息,还是有一定交互的(弹出验证码,输入账号密码登录等等)对话框. 而我们去看一下原生的对话框,虽然随着 ...

  8. android 自定义dialog样式,Android 自定义dialog类

    首先定制style样式 styles.xml 加入自定义样式 @null true true true @color/transparent @color/transparent true 0.6 在 ...

  9. Android弹出自定义Dialog,android自定义Dialog实现底部弹窗

    android自定义Dialog实现底部弹窗 拿到这个需求,很多人都是直接想用popWindow 实现,但是这样的效果我们完全可以根据系统的Dialog 自定义一个. AlertDialog.Buil ...

  10. android自定义dialog不显示,有关问题解决之Android自定义Dialog无法dismiss

    问题解决之Android自定义Dialog无法dismiss 场景: 点击ListView的一个Item,弹出自定义Dialog.在初始化Dialog时,将一个OnClickListener作为参数传 ...

最新文章

  1. MCMC(二)马尔科夫链
  2. Hotel California
  3. 在Linux下正确安装VMWARE TOOLS
  4. LeetCode Third Maximum Number
  5. MyBatis中使用#{}和${}的区别
  6. java中debug使用
  7. Spring.Resource与Spring资源获取方式
  8. java 手写缓存,java手写多级缓存
  9. 【poj题集整理】【存下来并不会看】
  10. Spring Data JPA: 实现自定义Repository
  11. 【leetcode】Max Points on a Line
  12. C 语言传递数组给函数的三种形式
  13. Python第十六课(模块3)
  14. 高并发业务接口开发思路(实战)
  15. win7系统数据库服务器,win7数据库 服务器
  16. 神舟k650d i5 d3安装EI Capitan问题总结
  17. android 经纬度距离计算器,经纬度距离角度计算软件|经纬度距离角度计算器(geography) v2.0免费版_星星软件园...
  18. BGA封装的优缺点解析
  19. TheDAO悲剧重演,SpankChain重入漏洞分析
  20. FXCM富汇官网:通过十个问题学习外汇知识

热门文章

  1. win7远程桌面怎么关闭计算机,Win7旗舰版系统远程桌面停止工作
  2. Spring @Autowired 知其然定需知其所以然 第一弹
  3. 【花雕体验】09 行空板硬件控制pinpong库的系列测试(之二)
  4. 如何恢复录音删除的录音文件_小达人点读笔教程5:如何使用点读笔录音
  5. 治愈拖延症患者的解药
  6. [JAVA]计算底面为正方形的长方体体积以及JAVA的math方法 2021-09-06
  7. linux全屏时钟应用下载,全屏时钟下载安装-全屏时钟app下载 苹果版v2.3-PC6苹果网...
  8. JAP中@Temporal
  9. 简约婚礼请柬PPT模板
  10. 共建安全大生态——2016第十七届信息安全大会采风