使用D-Pad的窗口管理器的焦点问题

问题描述

我正在使用窗口管理器在智能电视应用中进行通知,但在窗口上显示视图时遇到了一些问题,因此我无法处理焦点,也无法使用D-Pad获取子视图的单击事件。 / p>

我已经在Google上找到了,但没有找到解决此问题的任何方法。

使用窗口管理器显示通知视图:-

我们可以从Activity中调用此类:-
val notificationView = NotificationView(this) notificationView.addView()

科特琳代码:-NotificationView.kt

class NotificationView(val context: Context) {
    var notificationWM: WindowManager? = null
    var layoutParams: WindowManager.LayoutParams? = null
    fun addView(): View {
        val view: ViewGroup = LayoutInflater.from(context).inflate(
            R.layout.layout_overlay,null
        ) as ViewGroup

        if (notificationWM != null) {
            notificationWM!!.updateViewLayout(view,layoutParams)
        } else {
            notificationWM = getWindowManager()
            notificationWM!!.addView(view,layoutParams)
        }

        initView(view)
        return view
    }

    private fun getWindowManager(): WindowManager {
        val LAYOUT_FLAG: Int
        LAYOUT_FLAG = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
        } else {
            WindowManager.LayoutParams.TYPE_PHONE
        }
        val params = WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,LAYOUT_FLAG,WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,PixelFormat.TRANSLUCENT
        )
        params.gravity = Gravity.CENTER_VERTICAL
        params.gravity = Gravity.RIGHT or Gravity.TOP
        params.title = "Load Average"
        layoutParams = params
        return context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    }

    private fun initView(view: View) {

        val actionButton = view.findViewById<Button>(R.id.btn_action)
        val message = view.findViewById<TextView>(R.id.tv_message)

        actionButton.setOnClickListener {
            Toast.makeText(context,"Button Click Now",Toast.LENGTH_LONG).show()
        }
        actionButton.requestFocus()
    }

}

布局代码(此布局将在使用窗口管理器的通知中显示):-layout_overlay.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:background="@drawable/selector_notification_btn"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:fontFamily="@font/montserrat_regular"
        android:text="@string/my_profile"
        android:textSize="25dp" />

    <Button
        android:id="@+id/btn_action"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/login"
        android:focusableInTouchMode="true"
        android:focusable="true"
        android:background="@drawable/selector_notification_btn"
        android:fontFamily="@font/montserrat_regular" />
</LinearLayout>

通知视图显示完美,但是我没有使用D-Pad获得按钮的单击事件

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)