【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
    }

}
相关推荐
默_笙7 分钟前
🚊 别再让 AI 的记忆写两遍了:PostgreSQL 把关系查询和语义检索装进一张表
前端·javascript
lzhdim11 分钟前
C#不为人知的10个魔法特性:资深开发者也会震惊的底层奥秘
java·前端·javascript·算法·c#
weixin_3077791317 分钟前
C++代码实现MATLAB中的pade函数功能
开发语言·c++·算法·matlab
ProcessOn官方账号24 分钟前
java IO操作 你快速认识IO的相关基本知识
java·开发语言
神仙别闹41 分钟前
基于C++实现的(控制台)学校开课查询系统
开发语言·c++
只睡四小时1 小时前
JS 手写 D-pad 空间导航:电视端焦点引擎实战
android·开发语言·前端·javascript·ecmascript·hls·空间导航
2601_962218611 小时前
C++的几种编译器的实现
开发语言·c++
恋猫de小郭2 小时前
Android 原生的 Compose A2UI 也来了,你还抱着 XML 养老吗?
android·前端·flutter
程序媛_2 小时前
【JMeter】准备token文件
android·jmeter
多弗朗皮卡丘2 小时前
C++ stack和queue
开发语言·c++·deque·queue·stack·容器适配器