Android TV
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在 Android TV 上使用 D-Pad 導覽
Android TV 遙控器具有 D-pad 控制項,可傳送指令,這些指令會以按鍵事件的形式傳送至 Activity
的 dispatchKeyEvent(KeyEvent)
。請將這些項目委派給「PlayerView
」:
Kotlin
override fun dispatchKeyEvent(event: KeyEvent?): Boolean{
return playerView.dispatchKeyEvent(event!!) || super.dispatchKeyEvent(event)
}
Java
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
return playerView.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);
}
要求 PlayerView
的焦點對於瀏覽播放控制項和略過廣告非常重要。請考慮在 Activity
的 onCreate
中要求焦點:
Kotlin
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// ...
playerView.requestFocus()
// ...
}
Java
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
playerView.requestFocus();
// ...
}
如果您在 Android TV 上使用 Compose,則必須將 AndroidView
設為可聚焦,並將修飾符參數傳遞至 AndroidView
,據此委派事件:
AndroidView(
modifier = modifier
.focusable()
.onKeyEvent { playerView.dispatchKeyEvent(it.nativeKeyEvent) },
factory = { playerView }
)
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-08-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-27 (世界標準時間)。"],[],[],null,["D-pad navigation on Android TV\n\nThe remote control of Android TV has a D-pad control that sends commands that\narrive as key events at `dispatchKeyEvent(KeyEvent)` of your `Activity`. These\nneed to be delegated to the [`PlayerView`](/reference/androidx/media3/ui/PlayerView): \n\nKotlin \n\n```kotlin\noverride fun dispatchKeyEvent(event: KeyEvent?): Boolean{\n return playerView.dispatchKeyEvent(event!!) || super.dispatchKeyEvent(event)\n}\n```\n\nJava \n\n```java\n@Override\npublic boolean dispatchKeyEvent(KeyEvent event) {\n return playerView.dispatchKeyEvent(event) || super.dispatchKeyEvent(event);\n}\n```\n\n\u003cbr /\u003e\n\nRequesting focus for the `PlayerView` is important for navigating playback\ncontrols and skipping ads. Consider requesting the focus in `onCreate` of the\n`Activity`: \n\nKotlin \n\n```kotlin\noverride fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n // ...\n playerView.requestFocus()\n // ...\n}\n```\n\nJava \n\n```java\n@Override\npublic void onCreate(@Nullable Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n // ...\n playerView.requestFocus();\n // ...\n}\n```\n\n\u003cbr /\u003e\n\nIf you are using Compose on Android TV, you need to make the [`AndroidView`](/reference/kotlin/androidx/compose/ui/viewinterop/package-summary#AndroidView(kotlin.Function1,androidx.compose.ui.Modifier,kotlin.Function1))\nfocusable and delegate the event by passing the modifier parameter into the\n`AndroidView` accordingly: \n\n AndroidView(\n modifier = modifier\n .focusable()\n .onKeyEvent { playerView.dispatchKeyEvent(it.nativeKeyEvent) },\n factory = { playerView }\n )\n\n\u003cbr /\u003e"]]