[null,null,["最后更新时间 (UTC):2025-08-23。"],[],[],null,["# Focus in Compose\n\nWhen a user interacts with your app, they often do so by touching elements on\ntheir screen. However, this is not the only form of interaction. Other forms of\ninteraction could include the following:\n\n- A ChromeOS user might use the *arrow keys* on their physical keyboard to navigate the screen.\n- Someone playing a game could use their attached *game controller* to navigate through the game's menu.\n- A mobile app user might cycle through elements using the *on-screen keyboard*.\n\nIn these cases, it is important to track which component is active at any given\npoint in time, which is what we call **focus**. Elements on the screen should be\nfocused in a logical order. Jetpack Compose has a default way of handling focus\nthat is correct in most cases. However, in some cases, you might need to modify\nthis default behavior.\n\nThe following pages describe how to use focus in your app:\n\n- [Change focus traversal order](/develop/ui/compose/touch-input/focus/change-focus-traversal-order): Explains how to change the default focus order, add focus groups, and disable focus of a composable.\n- [Change focus behavior](/develop/ui/compose/touch-input/focus/change-focus-behavior): Describes how to request, capture, and release focus, and how to redirect focus upon entering a screen.\n- [React to focus](/develop/ui/compose/touch-input/focus/react-to-focus): Explains how to react to focus changes, add visual cues to elements, and understand the focus state of an element.\n\nDefault focus traversal order\n-----------------------------\n\nBefore we dive into the default behavior of the focus search, it's important to\nunderstand the concept of *level* in the hierarchy: generally speaking, we can\nsay that two `Composables` are at the same level when they are siblings, meaning\nthat they have the same parents. For instance, elements inside a `Column` are at\nthe same level. Getting up a level means going from a child to its `Composable`\nparent, or, keeping the same example, going back from an item to a `Column` that\ncontains it. Going down a level is the other way around, from the `Column`\nparent to the contained items. This concept can be applied to every `Composable`\nthat can contain other `Composables`.\n| **Note:** For information about manually controlling focus order, see [Control traversal order](/develop/ui/compose/accessibility/traversal).\n\nUI navigation can happen in multiple ways, some of which most users will already\nknow:\n\n- TABs: one-dimensional navigation, going *forward* or *backward* . TAB navigation advances focus to the next or previous element in the hierarchy. By default, Compose follows the declaration of the `Composables`. One-directional navigation can be achieved through the `tab` key on a keyboard, or the Rotary Bezel on a watch, and this kind of focus search will visit each element on the screen.\n- Arrow keys: two-dimensional navigation, going *left, right, up* , or *down*. Two-dimensional navigation can be achieved through a D-Pad on a TV or arrow keys on a keyboard, and its traversal order only visits elements at a given level. You can use the D-Pad center and Back button to go down and back up to a different level.\n\nTake as an example the screenshot below, where you have four buttons, one below\nthe other, and you want to cycle through them all in order of appearance.\nJetpack Compose delivers this behavior out of the box: the toolkit lets you\ncycle through each composable in vertical order from top to bottom using the\n`tab` key, or move focus by pressing the *up* or *down* arrow.\n**Figure 1**. List of buttons displayed in a small form factor\n\nWhen you switch to a different kind of layout, things change a bit. If your\nlayout has more than one column, like the layout below, Jetpack Compose lets you\nnavigate through them without having to add any code. If you press the `tab`\nkey, Jetpack Compose automatically highlights the items in order of declaration,\nfrom First to Fourth. Using arrow keys on your keyboard makes the selection\nfollow the desired direction in 2D space.\n\n\n```kotlin\nColumn {\n Row {\n TextButton({ }) { Text(\"First field\") }\n TextButton({ }) { Text(\"Second field\") }\n }\n Row {\n TextButton({ }) { Text(\"Third field\") }\n TextButton({ }) { Text(\"Fourth field\") }\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/focus/FocusSnippets.kt#L82-L91\n```\n\n\u003cbr /\u003e\n\nThe `Composables` are declared in two `Rows`, and the focus elements are\ndeclared in order, from first to fourth. When you press the `tab` key, this\nproduces the following focus order:\n**Figure 2**. List of buttons placed in two columns side by side in a bigger form factor\n\nIn the snippet below, you declare the items in `Columns` rather than in `Rows`:\n\n\n```kotlin\nRow {\n Column {\n TextButton({ }) { Text(\"First field\") }\n TextButton({ }) { Text(\"Second field\") }\n }\n Column {\n TextButton({ }) { Text(\"Third field\") }\n TextButton({ }) { Text(\"Fourth field\") }\n }\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/focus/FocusSnippets.kt#L101-L110\n```\n\n\u003cbr /\u003e\n\nThis layout traverses the items vertically, from top to bottom, from the start\nof the screen towards the end:\n**Figure 3**. List of buttons placed in two columns side by side in a bigger form factor\n\nThe previous two samples, while differing in one-directional navigation, provide\nthe same experience when it comes to two-dimensional navigation. This is usually\nbecause the items on the screen have the same geographic placement in both\nexamples. Navigating right from the first `Column` moves the focus to the\nsecond, and navigating down from the first `Row` moves the focus to the one\nbelow it.\n| **Note:** Focus properties are a special case, in which the parents always win in case of collisions or duplicates.This is something to keep in mind when working with focus.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Change focus behavior](/develop/ui/compose/touch-input/focus/change-focus-behavior)\n- [ConstraintLayout in Compose](/develop/ui/compose/layouts/constraintlayout)\n- [Flow layouts in Compose](/develop/ui/compose/layouts/flow)"]]