Android TV 的自訂檢視畫面支援功能

雖然許多 Android TV 應用程式都內建 Android 元件, 也要考量 架構或元件,尤其是使用自訂檢視時。

與 OpenGL 或 Canvas 直接互動的自訂檢視畫面元件可能無法正常運作 以及 TalkBack 和切換控制功能等無障礙服務

請思考切換 TalkBack 時可能發生的問題,如下所示 已開啟:

  • 應用程式中的無障礙功能焦點 (綠色矩形) 可能會消失。
  • 無障礙焦點可能會選取整個螢幕的邊界。
  • 你可能無法移動無障礙焦點。
  • D-pad 的四個方向鍵可能不會產生任何作用,即使程式碼會處理這些按鍵也一樣。

在應用程式中發現這些問題時,請確認 應用程式會公開其 AccessibilityNodeInfo 存取無障礙服務樹狀結構

本指南的其餘部分提供瞭解決這些問題的解決方案和最佳做法。

無障礙服務會使用 D-pad 事件

這個問題的根本原因在於,無障礙功能會消耗重要事件 免費 Google Cloud 服務

Dpad 事件使用量 圖 1. 這張圖表顯示系統如何在開啟/關閉 TalkBack 的情況下運作。

如圖 1 所示,當 TalkBack 開啟時,D-Pad 事件會觸發 D-Pad 事件 不會傳遞至開發人員定義的 D-Pad 處理常式。 無障礙服務會接收按鍵事件,因此能夠 無障礙中心的重點由於自訂 Android 元件並不會預設公開發布 無障礙服務針對自己在畫面上的位置取得相關資訊。 無障礙服務無法移動無障礙功能的焦點來醒目顯示該元素。

其他無障礙服務也很類似:D-Pad 事件也可能 。

因為 D-pad 事件會提交至無障礙服務 服務不知道 UI 元件在自訂檢視畫面中的位置 您必須為應用程式實作 AccessibilityNodeInfo,才能傳送 正確設定重要事件

向無障礙服務公開資訊

為無障礙服務提供 自訂檢視區塊的位置和說明,請導入 AccessibilityNodeInfo 顯示每個元件的詳細資料 定義 View 的邏輯關係,讓無障礙服務可以 管理焦點,導入 ExploreByTouchHelper 並使用 ViewCompat.setAccessibilityDelegate(View, AccessibilityDelegateCompat)。 以及自訂檢視畫面

實作 ExploreByTouchHelper 時,請覆寫其四個抽象方法:

Kotlin

// Return the virtual view ID whose view is covered by the input point (x, y).
protected fun getVirtualViewAt(x: Float, y: Float): Int

// Fill the virtual view ID list into the input parameter virtualViewIds.
protected fun getVisibleVirtualViews(virtualViewIds: List<Int>)

// For the view whose virtualViewId is the input virtualViewId, populate the
// accessibility node information into the AccessibilityNodeInfoCompat parameter.
protected fun onPopulateNodeForVirtualView(virtualViewId: Int, @NonNull node: AccessibilityNodeInfoCompat)

// Set the accessibility handling when perform action.
protected fun onPerformActionForVirtualView(virtualViewId: Int, action: Int, @Nullable arguments: Bundle): Boolean

Java

// Return the virtual view ID whose view is covered by the input point (x, y).
protected int getVirtualViewAt(float x, float y)

// Fill the virtual view ID list into the input parameter virtualViewIds.
protected void getVisibleVirtualViews(List<Integer> virtualViewIds)

// For the view whose virtualViewId is the input virtualViewId, populate the
// accessibility node information into the AccessibilityNodeInfoCompat parameter.
protected void onPopulateNodeForVirtualView(int virtualViewId, @NonNull AccessibilityNodeInfoCompat node)

// Set the accessibility handling when perform action.
protected boolean onPerformActionForVirtualView(int virtualViewId, int action, @Nullable Bundle arguments)

如需瞭解詳情,請觀看 2013 年 Google I/O 大會 - 啟用失明和低視能 Android 的無障礙功能 或進一步瞭解如何填入無障礙功能事件

最佳做法

範例

請參閱 Android TV 的自訂檢視畫面無障礙功能範例,瞭解最佳做法 為使用自訂檢視畫面的應用程式新增無障礙功能支援。