【问题标题】:how to make android TV app accesiible by DPAD?如何使 DPAD 可以访问 android TV 应用程序?
【发布时间】:2021-01-27 03:13:10
【问题描述】:

我已经为 Android TV 创建了一个 Android 应用,我想知道如何启用 D-pad 导航到我的菜单。 我是 android TV 应用程序的新手,找不到任何其他方法来做同样的事情。 对于菜单,我添加了 4 个 TextView,但 D-Pad 无法访问它们,有没有其他方法可以让 DPAD 访问它们,或者我应该为此使用任何其他视图。

这是布局文件:

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

【问题讨论】:

标签: android android-studio android-tv google-tv d-pad


【解决方案1】:

一般来说,您不希望将TextView 用于此类事情。在我看来,这些视图应该是Buttons,因为您希望用户与它们进行交互。使用Button 还可以更好地实现可访问性,并且您始终可以为按钮设置样式以显示您想要的样式。

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

【讨论】:

    【解决方案2】:

    实现key listener来实现这个东西,就像click listener一样。例如:

       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 中添加这些来添加这些,但这不是必需的,它应该在没有这些的情况下工作

    【讨论】:

      猜你喜欢
      • 2019-01-24
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-12-23
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      相关资源
      最近更新 更多