【Android】创建一个可以一直显示但是又不影响用户页面操作的Dialog

需求

此Dialog作为通知类型的dialog,当某事件被触发的时候,显示此Dialog,在dialog显示的时候,用户仍然可以正常操作页面,点击事件等不会被此dialog拦截。

实现

首先创建一个布局文件:这个根据自己需求创建(代码省略)

文件命名为【layout_notification_always_show】

创建dialog

kotlin 复制代码
class NotificationAlwaysShow(private val mContext: Context)
    : INotificationDialog {

    private var mDialog: Dialog? = null
    private var mTopTitle = mContext.getString(R.string.notification_caution)
    private var mBottomMessage = mContext.getString(R.string.notification_caution_message)
    private var mImage = R.drawable.ic_back_up

    /**
     * 设置图片
     */
    fun setImage(res: Int): NotificationAlwaysShow {
        mImage = res
        return this
    }

    /**
     * 设置提示Title
     */
    fun setTitle(title: Int): NotificationAlwaysShow {
        mTopTitle = mContext.getString(title)
        return this
    }

    /**
     * 设置详细提示信息
     */
    fun setMessage(message: Int): NotificationAlwaysShow {
        mBottomMessage = mContext.getString(message)
        return this
    }

    override fun show() {
        mDialog = Dialog(mContext)
        mDialog?.setContentView(R.layout.layout_notification_always_show)
        mDialog?.setCancelable(false)
        mDialog?.setCanceledOnTouchOutside(false)

        val window = mDialog?.window
        val layoutParams = window?.attributes
        layoutParams?.gravity = Gravity.TOP
        //通过设置Flag来实现我们的需求
        layoutParams?.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
                WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
        layoutParams?.y = mContext.resources.getDimensionPixelSize(R.dimen.notification_top_width)
        window?.attributes = layoutParams
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setDimAmount(0f)
        window?.setWindowAnimations(R.style.Notification_dialog_animation)

        val ivImage = mDialog?.findViewById<ImageView>(R.id.iv_iamge_always_show)
        val textTitle = mDialog?.findViewById<TextView>(R.id.tv_title_always_show)
        val textMessage = mDialog?.findViewById<TextView>(R.id.tv_message_always_show)

        ivImage?.setImageResource(mImage)
        textTitle?.text = mTopTitle
        textMessage?.text = mBottomMessage

        // 设置按键监听器,拦截返回键事件
//        mDialog?.setOnKeyListener(object : DialogInterface.OnKeyListener {
//            override fun onKey(dialog: DialogInterface?, keyCode: Int, event: KeyEvent?): Boolean {
//                if (keyCode == KeyEvent.KEYCODE_BACK && event?.action == KeyEvent.ACTION_UP) {
//                    return true
//                }
//                return false
//            }
//        })

        mDialog?.show()
    }

    override fun dismiss() {
        mDialog?.dismiss()
    }

    override fun showing(): Boolean {
        return mDialog?.isShowing == true
    }

}
相关推荐
开源技术2 分钟前
Python GeoPandas基础知识:地图、投影和空间连接
开发语言·ide·python
hedley(●'◡'●)5 分钟前
基于cesium和vue的大疆司空模仿程序
前端·javascript·vue.js·python·typescript·无人机
Cult Of5 分钟前
Alicea Wind的个人网站开发日志(2)
开发语言·python·vue
百思可瑞教育7 分钟前
构建自己的Vue UI组件库:从设计到发布
前端·javascript·vue.js·ui·百思可瑞教育·北京百思教育
我找到地球的支点啦11 分钟前
通信扩展——扩频技术(超级详细,附带Matlab代码)
开发语言·matlab
微小冷29 分钟前
Rust异步编程详解
开发语言·rust·async·await·异步编程·tokio
CappuccinoRose33 分钟前
JavaScript 学习文档(二)
前端·javascript·学习·数据类型·运算符·箭头函数·变量声明
A9better33 分钟前
C++——不一样的I/O工具与名称空间
开发语言·c++·学习
清水白石00836 分钟前
《为什么说 deque 是 Python 滑动窗口的“隐藏神器”?深入解析双端队列的高效之道》
开发语言·python
杜子不疼.37 分钟前
Ascend_C自定义算子开发
c语言·开发语言