[Android] Create a Dialog that can be displayed all the time but does not affect user page operations

need

This Dialog is used as a notification type dialog. When an event is triggered, this Dialog is displayed. When the dialog is displayed, the user can still operate the page normally, and click events will not be intercepted by this dialog.

accomplish

First create a layout file: Create this according to your own needs (code omitted)
The file is named [layout_notification_always_show]

Create dialog

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
    }

}

Guess you like

Origin blog.csdn.net/qq_43358469/article/details/134380829