自定义弹框的简单写法Demo

效果

源码

Koltin
CustomDialog

package com.sq.module.widgetimport android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.sq.module.R/*** @ClassName ExitDialog* @Description TODO* @Author Kolin Zhao* @Date 2021/6/3 20:45* @Version 1.0*/
class CustomDialog : Dialog {constructor(context: Context?) : super(context!!)constructor(context: Context?, theme: Int) : super(context!!, theme)class Builder(private val context: Context) {var content: String? = nullprivate var positiveButtonClickListener: DialogInterface.OnClickListener? = nullprivate var negativeButtonClickListener: DialogInterface.OnClickListener? = nullfun setContent(content: String): Builder {this.content = contentreturn this}fun setPositiveButton(listener: DialogInterface.OnClickListener): Builder {this.positiveButtonClickListener = listenerreturn this}fun setNegativeButton(listener: DialogInterface.OnClickListener): Builder {this.negativeButtonClickListener = listenerreturn this}fun create(): CustomDialog {val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater//为自定义弹框设置主题val customDialog = CustomDialog(context, R.style.CustomDialog)val view = layoutInflater.inflate(R.layout.dialog_exit, null)customDialog.addContentView(view, LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT))//设置弹框内容content?.let {(view.findViewById(R.id.dialog_content) as TextView).text = it}//设置弹框按钮positiveButtonClickListener?.let {(view.findViewById(R.id.dialog_sure) as Button).setOnClickListener {positiveButtonClickListener!!.onClick(customDialog, DialogInterface.BUTTON_POSITIVE)}} ?: run {(view.findViewById(R.id.dialog_sure) as Button).visibility = View.GONE}negativeButtonClickListener?.let {(view.findViewById(R.id.dialog_cancel) as Button).setOnClickListener {negativeButtonClickListener!!.onClick(customDialog, DialogInterface.BUTTON_NEGATIVE)}} ?: run {(view.findViewById(R.id.dialog_cancel) as Button).visibility = View.GONE}customDialog.setContentView(view)return customDialog}}
}

调用

val builder = CustomDialog.Builder(this)
builder.setPositiveButton(DialogInterface.OnClickListener { dialogInterface, _ ->dialogInterface.dismiss()//具体逻辑
})
builder.setNegativeButton(DialogInterface.OnClickListener { dialogInterface, _ ->dialogInterface.dismiss()//具体逻辑
})
builder.create().show()

Layout
dialog_exit.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/transparent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="@dimen/marginDialog"android:background="@drawable/champer_form"android:orientation="vertical"android:padding="20dp"><TextViewandroid:id="@+id/dialog_content"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginVertical="36dp"android:text="确定要退出账号?"tools:ignore="HardcodedText"android:textColor="@color/ThemeDarkPurple"android:textSize="@dimen/title"android:layout_gravity="center"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/dialog_cancel"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginEnd="6dp"android:layout_weight="1"android:background="@drawable/exit_cancel"android:text="取 消"android:textColor="@color/ThemeBlue"android:textSize="20sp"tools:ignore="HardcodedText" /><Buttonandroid:id="@+id/dialog_sure"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:background="@drawable/exit_sure"android:text="确定"android:textColor="@color/white"android:textSize="20sp"tools:ignore="HardcodedText"android:layout_marginStart="6dp"/></LinearLayout></LinearLayout></FrameLayout>

champer_form.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="@color/white"/><corners android:radius="6dp"/>
</shape>

styles.xml

    <!--退出自定义弹框--><style name="CustomDialog" parent="Theme.AppCompat.Dialog"><item name="android:background">@color/transparent</item><item name="android:windowBackground">@color/transparent</item><item name="windowNoTitle">true</item><item name="android:windowIsFloating">true</item></style>

【Android,Kotlin】自定义弹框的简单写法Demo相关推荐

  1. android 系统弹框与自定义弹框

    1. 系统弹框 private AlertDialog alertDialog; private void tipDialog(String value) {         if (alertDia ...

  2. 自定义弹框一(PopupWindow实现) 响应两种点击事件

    自定义弹框通常可以使用 自定义的popupwindow,自定义的activity,自定义的dialog来实现  这里先用popupwindow来实现 并且可以在弹框上实现点击事件 或者在其所依附的ac ...

  3. 一个常用的自定义弹框封装(适配 AndroidX),加载 ProgressDialog,状态显示的 StatusDialog 和自定义 Toast,全部支持背景颜色,圆角,边框和文字的自定义,构建者模

    MNProgressHUD 项目地址:maning0303/MNProgressHUD  简介: 一个常用的自定义弹框封装(适配 AndroidX),加载 ProgressDialog,状态显示的 S ...

  4. android仿ios弹框_在“提示”框中:iOS外观(在Android上运行),Google Maps作为Time Machine,下载Wii游戏保存...

    android仿ios弹框 Once a week we round up some great reader tips and share them with everyone. Read on t ...

  5. Android中MaterialSearchView(搜索框)的简单实用

    Android中MaterialSearchView(搜索框)的简单实用 收搜框架有好多,动画效果满炫酷的就是代码太多,没发抽取他的搜索功能,而这个却比较好用些,直接从代码里面抠出来一个简单实现搜索功 ...

  6. 抖音短视频系统开发自定义弹框

    我们如果想要实现这样的自定义弹框,需要怎么做呢,接下来就是教程 首先,我们需要创建一个类继承Dialog 下面就是代码 import android.annotation.SuppressLint; ...

  7. android 自定义搜索框edittext,Android编程自定义搜索框实现方法【附demo源码下载】...

    本文实例讲述了Android编程自定义搜索框实现方法.分享给大家供大家参考,具体如下: 先来看效果图吧~ 分析:这只是模拟了一个静态数据的删除与显示 用EditText+PopupWindow+lis ...

  8. react离开页面,自定义弹框拦截,路由拦截

    前言: 项目有个需求是:跳转路由,在离开页面前,需要弹框询问用户是否确定离开. 用react-router的<Prompt>组件是可以的,但是,怎么使用antd组件(或者说自定义组件)呢? ...

  9. element-ui 自定义弹框,加入图片

    element 组件库里面有弹框组件. 实际项目中需要用到弹框.需求是在弹框的div里面放入一张图片. 用到的是element弹框组件的最后一个.自定义弹框组件 const h = this.$cre ...

  10. jq js 自定义弹框

    自定义弹框 <body> <script type="text/javascript" src="./js/jquery-1.8.3.js"& ...

最新文章

  1. think php a方法,ThinkPHP之A方法实例讲解_PHP
  2. (转)Maven学习总结(七)——eclipse中使用Maven创建Web项目
  3. Linux(debian7)操作基础(十四)之文本搜索命令grep使用方法
  4. python模块和类和方法_Python类、模块、包的区别
  5. 水上运动鞋行业调研报告 - 市场现状分析与发展前景预测
  6. 20220228:力扣第282场周赛(下)
  7. 【原创】PDA 实现DataGrid可编辑
  8. 关于FPGA软件quartus仿真出现cannot launch the modelsim software问题的解决
  9. js实现文件上传、文件预览、拖拽上传的方法
  10. 用图片来搜索 教你玩转Google按图搜索
  11. 我和权威的故事——王垠
  12. python抽奖概率设计_python 抽奖概率
  13. Newton 3 牛顿动力学插件 - 主体属性面板
  14. iMessage推广(群发)技术实现
  15. 信息系统安全开发注意事项(一)
  16. 风蚀侵蚀力计算在python上的实现
  17. UDS之浅谈31服务
  18. python 凸多边形面积
  19. 戴尔R730服务器增加内存,多功能存储密集型 戴尔R730xd拆解评测
  20. PHP友情链接检测,www.jsphp.net友情链接查询结果 - 站长工具

热门文章

  1. 联通托管服务器为什么打不开网页,为什么有些网页联通宽带打不开,移动4G能打开?...
  2. matlab怎么fprintf,matlab中的fprintf函数怎么用
  3. 批处理为win7桌面添加计算机图标,WIN7桌面显示IE图标bat
  4. yamada算法_脉宽调制中的颤振算法
  5. 怎样实现VLAN间通信,三种解决方案,一节课带你掌握
  6. 文件或目录损坏且无法读取android studio
  7. java 实现macd算法_macd 的java版本实现 包含测试用例
  8. 图片处理——使用NDK添加文字和图片水印
  9. 共享打印机服务器脱机状态,打印机脱机工作怎么恢复 共享的打印机脱机状态...
  10. mac 安装虚拟机win11