如何使DPAD可以使用android TV应用程序?

问题描述

我已经为Android TV创建了一个Android应用,我想知道如何在菜单上启用D-pad导航功能。 我是Android TV应用程序的新手,无法找到其他方法来执行此操作。 对于菜单,我添加了4个TextView,但是D-Pad无法使用它们,是否有其他方法可以使DPAD访问它们,或者我应该为此使用其他View。

这是布局文件

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/mainframe"
        android:layout_width="810dp"
        android:layout_height="538dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/linearLayout"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="148dp"
        android:layout_height="0dp"
        android:layout_marginTop="1dp"
        android:layout_marginBottom="1dp"
        android:background="@color/colorPrimaryDark"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

        <TextView
            android:id="@+id/NIYA"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="20dp"
            android:fontFamily="cursive"
            android:nextFocusDown="@+id/livePreview"
            android:text="NIYA"
            android:textColor="@color/colorPrimary"
            android:textSize="30sp" />

        <TextView
            android:id="@+id/livePreview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:fontFamily="@font/montserrat"
            android:gravity="center_horizontal"
            android:nextFocusDown="@+id/AddDevice"
            android:text="Live Preview"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/AddDevice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:fontFamily="@font/montserrat"
            android:gravity="center_horizontal"
            android:nextFocusDown="@+id/deviceManager"
            android:text="Add A Device"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/deviceManager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:fontFamily="@font/montserrat"
            android:gravity="center_horizontal"
            android:nextFocusDown="@+id/chooseStorage"
            android:text="Device Manager"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/chooseStorage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:fontFamily="@font/montserrat"
            android:gravity="center_horizontal"
            android:text="Choose Storage"
            android:textColor="@color/colorPrimary"
            android:textSize="18sp" />
  </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

解决方法

通常,您不想使用TextView这样的东西。在我看来,这些视图本来就是Button,因为您希望用户与其进行交互。使用Button还可以改善可访问性,并且您随时可以设置Button的样式,以显示所需的样式。

布局不起作用的原因是默认情况下TextView不能聚焦。尽管我强烈建议在这种情况下使用Button,但是您可以通过添加android:focusable="true"或通过在运行时使用setFocusable setter方法将focusable设置为true来使TextView在XML中具有焦点。只需注意,您需要确保View本身具有显示其重点的方式(例如StateListDrawable)。

,

实施关键侦听器以实现此目的,就像单击侦听器一样。例如:

   yourTextview.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v,int keyCode,KeyEvent event) {
            //TODO Auto-generated method stub
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    return true;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    return true;
                }  if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                    return true;
                }  if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                    return true;
                }
                else if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    return true;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                    return true;
                }
            }
            return false;
        }
    }); 

在每个动作的if语句中执行想要执行的任何操作。如果您的控件未对按键事件做出响应,则可以通过将其添加到xml android:focusable =“ true”或android:clickable true中来添加这些控件,但这不是必需的,没有这些控件就可以工作

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...