1e41f4b71Sopenharmony_ci# Focus Event
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Basic Concepts and Specifications
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### Basic Concepts
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci**Focus, Focus Chain, and Focus Traversal**
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci- Focus: refers to the single interactive element on the current application screen. When users interact indirectly with the application using non-pointing input devices such as keyboards, TV remote controls, or in-car joysticks/knobs, navigation and interaction based on focus are crucial means of input.
10e41f4b71Sopenharmony_ci- Focus chain: refers to the sequence of nodes from the root to a focused component in the application's component tree, where all nodes are considered focused.
11e41f4b71Sopenharmony_ci- Focus traversal: refers to the behavior of focus shifting between components in an application. This process is transparent to the user but can be monitored through **onFocus** and **onBlur** events. For details on how focus traversal is managed, see [Focus Traversal Guidelines](#focus-traversal-guidelines)
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci**Focus State**
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ciRefers to the style indicating the currently focused component.
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci- Display rules: The focus state is not displayed by default; it only appears when the application is active. A component with the focus state is definitely focused, but not all focused components show the state, depending on the activation status. Most components come with default focus state styles; you customize these styles when needed. Once customized, the component will no longer display the default focus state style. In a focus chain, if multiple components have the focus state, the system shows the focus state for only one, prioritizing the child component's focus state over others.
19e41f4b71Sopenharmony_ci- Entering the activated state: Pressing the Tab key on an external keyboard activates focus, allowing subsequent use of the **Tab** key or arrow keys for focus traversal. The initial **Tab** press that activates focus does not cause focus to move.
20e41f4b71Sopenharmony_ci- Exiting the activated state: Focus activation ends upon any click event, including touch screen presses or mouse clicks.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci
23e41f4b71Sopenharmony_ci**Hierarchical Pages**
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ciHierarchical pages are specialized container components, such as **Page**, **Dialog**, **SheetPage**, **ModalPage**, **Menu**, **Popup**, **NavBar**, and **NavDestination**, within a focus framework. These components typically have the following key features:
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci- Visual layering: They appear on top of other content, creating a distinct visual hierarchy.
28e41f4b71Sopenharmony_ci- Focus capture: They automatically take focus when first displayed.
29e41f4b71Sopenharmony_ci- Focus limitation: When focus is within these components, users cannot use keyboard keys to move focus outside to other elements. In other words, focus movement is confined within the component.
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ciAn application always has at least one hierarchical page in focus. When this hierarchical page is closed or no longer visible, the focus shifts to another, ensuring smooth user interaction.
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci> **NOTE**
36e41f4b71Sopenharmony_ci>
37e41f4b71Sopenharmony_ci> The **Popup** component does not capture focus if it has **focusable** set to **false**.
38e41f4b71Sopenharmony_ci>
39e41f4b71Sopenharmony_ci> The **NavBar** and **NavDestination** components do not restrict focus movement and share the focus scope of their immediate parent hierarchical page.
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci**Root Container**
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ciIn hierarchical pages, the root container is where the default focus resides when the page is first shown.
48e41f4b71Sopenharmony_ci
49e41f4b71Sopenharmony_ciYou can change the default focus using the **defaultFocus** attribute.
50e41f4b71Sopenharmony_ci
51e41f4b71Sopenharmony_ciPressing **Tab** with focus on the root container activates focus and passes it to child components. Focus proceeds to the last focused child or the first child if no previous focus exists, until it reaches the leaf node.
52e41f4b71Sopenharmony_ci
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci### Focus Traversal Guidelines
56e41f4b71Sopenharmony_ci
57e41f4b71Sopenharmony_ciFocus traversal can be divided into active and passive based on how it is triggered.
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_ci**Active Focus Traversal**
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ciActive focus traversal refers to focus movement initiated by deliberate actions, such as keyboard shortcuts (**Tab**, **Shift+Tab**, arrow keys) and programmatic focus control through **requestFocus**, **clearFocus**, and **focusOnTouch**.
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci- Keyboard traversal
68e41f4b71Sopenharmony_ci1. Prerequisite: The application is in the focus activated state.
69e41f4b71Sopenharmony_ci2. Scope: limited to the currently focused hierarchical page, as detailed in the "Focus limitation" section under "Hierarchical Pages."
70e41f4b71Sopenharmony_ci3. Key types:
71e41f4b71Sopenharmony_ci**Tab** key: follows a Z-shaped logic to traverse all leaf nodes within the scope, looping back to the first after the last.
72e41f4b71Sopenharmony_ci**Shift+Tab**: reverses the direction of the **Tab** key.
73e41f4b71Sopenharmony_ciArrow keys (up, down, left, and right): moves focus in a cross-shaped pattern, with container-specific algorithms determining the next focus in a single-layer container. If the algorithm determines the next focus should be on a container component, the system uses a center-point distance priority algorithm to further identify the target child node within the container.
74e41f4b71Sopenharmony_ci4. Traversal algorithm: Each focusable container has a unique algorithm defining how focus moves.
75e41f4b71Sopenharmony_ci5. Priority: Child components take precedence in handling keyboard events over parents.
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci- requestFocus
78e41f4b71Sopenharmony_ciMoves focus to a specific component, which is allowed across hierarchical pages but not across windows or different ArkUI instances.
79e41f4b71Sopenharmony_ciFor details, see [Active Focus Acquisition/Loss](#active-focus-acquisitionloss).
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci- clearFocus
82e41f4b71Sopenharmony_ciClears the focus within the current hierarchical page, with the focus reverting to the root container. For details, see [clearFocus](../reference/apis-arkui/arkui-ts/ts-universal-attributes-focus.md#clearfocus12).
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci- focusOnTouch
85e41f4b71Sopenharmony_ciEnables a component to gain focus on touch. It is ineffective on non-focusable components. For container components, focus goes to the last focused child or the first focusable child upon touch. For details, see [focusOnTouch](../reference/apis-arkui/arkui-ts/ts-universal-attributes-focus.md#focusontouch9).
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**Passive Focus Traversal**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ciPassive focus traversal occurs when the focus automatically shifts due to system actions or other operations without developer intervention, reflecting the default behavior of the focus system.
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci
93e41f4b71Sopenharmony_ciMechanisms that trigger passive focus traversal include:
94e41f4b71Sopenharmony_ci
95e41f4b71Sopenharmony_ci- Component removal: If a focused component is removed, the system tries to shift focus to the next available sibling, following a back-to-front order. If no siblings are focusable, focus is released to the parent component.
96e41f4b71Sopenharmony_ci- Attribute change: Changing a component's **focusable** or **enabled** to **false**, or **visibility** to invisible causes the system to automatically move focus to another focusable component, using the same method as for component removal.
97e41f4b71Sopenharmony_ci- Hierarchical page transition: During switching between hierarchical pages, the current page's focus is automatically released, and the new page may automatically gain focus according to preset logic.
98e41f4b71Sopenharmony_ci- **Web** component initialization: The **Web** component may immediately gain focus upon creation if designed to do so (for example, certain dialog boxes or text boxes), which is part of the component's behavior and not governed by the focus framework specifications.
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci### Focus Traversal Algorithms
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ciIn the focus management system, every focusable container is assigned a specific algorithm that dictates how focus moves from the current to the next focusable child component when **Tab**, **Shift+Tab**, or arrow keys are used.
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ciThe algorithm used by a container is based on its UX design and is implemented by the component itself. The focus framework supports three focus traversal algorithms: linear, projection, and custom.
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci**Linear Focus Traversal Algorithm**
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci
113e41f4b71Sopenharmony_ciThe linear focus traversal algorithm is the default algorithm, focusing on the order of child nodes in the node tree, commonly used in single-direction layouts such as **Row**, **Column**, and **Flex** containers. Its operation rules are as follows:
114e41f4b71Sopenharmony_ci
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci- Order dependency: The focus order is based solely on the mounting sequence of child nodes in the node tree, independent of their visual layout.
117e41f4b71Sopenharmony_ci- **Tab** key traversal: The **Tab** key moves focus through nodes in their mounting sequence.
118e41f4b71Sopenharmony_ci- Arrow key traversal: Arrow keys perpendicular to the container's layout direction are ignored. For example, a horizontal **Row** container does not accept focus requests from up and down keys.
119e41f4b71Sopenharmony_ci- Boundary handling: The container rejects focus requests in the opposite direction from the current focus edge. For example, if the focus is on the first child of a horizontal **Row** container, it won't process leftward focus requests.
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci
122e41f4b71Sopenharmony_ci**Projection Focus Traversal Algorithm**
123e41f4b71Sopenharmony_ci
124e41f4b71Sopenharmony_ciThe projection focus traversal algorithm determines the next focus based on the overlap area and center-point distance of the projection of the current focused component in the direction of focus movement. It is particularly suitable for containers with varying child sizes, such as the **Flex** component with the **wrap** attribute. Its operation rules are as follows:
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci
127e41f4b71Sopenharmony_ci- Arrow keys: Focus goes to the child with the largest overlap area and the shortest center-point distance to the projection of the current focus. If multiple children qualify, the first in the node tree is chosen. If no components overlap with the projection, the focus request is unprocessable.
128e41f4b71Sopenharmony_ci- **Tab** key: It mimics a rightward shift to find the next focus; if none is available, it simulates moving the current focus down its height, and then checks leftward. The child farthest in the direction with overlapping projection wins.
129e41f4b71Sopenharmony_ci- **Shift+Tab** key: It mimics a leftward shift to find the next focus; if none is available, it simulates moving the current focus up its height, and then checks rightward. The child farthest in the direction with overlapping projection wins.
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci
132e41f4b71Sopenharmony_ci**Custom Focus Traversal Algorithm**
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ciThe custom focus traversal algorithm is defined by the component itself, allowing for specific focus traversal behaviors as determined by the component's design specifications.
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci## onFocus/onBlur Events
137e41f4b71Sopenharmony_ci
138e41f4b71Sopenharmony_ci```ts
139e41f4b71Sopenharmony_cionFocus(event: () => void)
140e41f4b71Sopenharmony_ci```
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci
143e41f4b71Sopenharmony_ciTriggered when the bound component obtains focus.
144e41f4b71Sopenharmony_ci
145e41f4b71Sopenharmony_ci```ts
146e41f4b71Sopenharmony_cionBlur(event:() => void)
147e41f4b71Sopenharmony_ci```
148e41f4b71Sopenharmony_ci
149e41f4b71Sopenharmony_ciTriggered when the bound component loses focus.
150e41f4b71Sopenharmony_ci
151e41f4b71Sopenharmony_ciThe **onFocus** and **onBlur** APIs are usually used in pairs to listen for the focus changes of the component.
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci```ts
154e41f4b71Sopenharmony_ci// xxx.ets
155e41f4b71Sopenharmony_ci@Entry
156e41f4b71Sopenharmony_ci@Component
157e41f4b71Sopenharmony_cistruct FocusEventExample {
158e41f4b71Sopenharmony_ci  @State oneButtonColor: Color = Color.Gray;
159e41f4b71Sopenharmony_ci  @State twoButtonColor: Color = Color.Gray;
160e41f4b71Sopenharmony_ci  @State threeButtonColor: Color = Color.Gray;
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci  build() {
163e41f4b71Sopenharmony_ci    Column({ space: 20 }) {
164e41f4b71Sopenharmony_ci      // You can use the up and down arrow keys on an external keyboard to move the focus between the three buttons. When a button gains focus, its color changes. When it loses focus, its color changes back.
165e41f4b71Sopenharmony_ci      Button('First Button')
166e41f4b71Sopenharmony_ci        .width(260)
167e41f4b71Sopenharmony_ci        .height(70)
168e41f4b71Sopenharmony_ci        .backgroundColor(this.oneButtonColor)
169e41f4b71Sopenharmony_ci        .fontColor(Color.Black)
170e41f4b71Sopenharmony_ci          // Listen for the focus obtaining event of the first component and change its color when it obtains focus.
171e41f4b71Sopenharmony_ci        .onFocus(() => {
172e41f4b71Sopenharmony_ci          this.oneButtonColor = Color.Green;
173e41f4b71Sopenharmony_ci        })
174e41f4b71Sopenharmony_ci          // Listen for the focus loss event of the first component and change its color when it loses focus.
175e41f4b71Sopenharmony_ci        .onBlur(() => {
176e41f4b71Sopenharmony_ci          this.oneButtonColor = Color.Gray;
177e41f4b71Sopenharmony_ci        })
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci      Button('Second Button')
180e41f4b71Sopenharmony_ci        .width(260)
181e41f4b71Sopenharmony_ci        .height(70)
182e41f4b71Sopenharmony_ci        .backgroundColor(this.twoButtonColor)
183e41f4b71Sopenharmony_ci        .fontColor(Color.Black)
184e41f4b71Sopenharmony_ci          // Listen for the focus obtaining event of the second component and change its color when it obtains focus.
185e41f4b71Sopenharmony_ci        .onFocus(() => {
186e41f4b71Sopenharmony_ci          this.twoButtonColor = Color.Green;
187e41f4b71Sopenharmony_ci        })
188e41f4b71Sopenharmony_ci          // Listen for the focus loss event of the second component and change its color when it loses focus.
189e41f4b71Sopenharmony_ci        .onBlur(() => {
190e41f4b71Sopenharmony_ci          this.twoButtonColor = Color.Grey;
191e41f4b71Sopenharmony_ci        })
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci      Button('Third Button')
194e41f4b71Sopenharmony_ci        .width(260)
195e41f4b71Sopenharmony_ci        .height(70)
196e41f4b71Sopenharmony_ci        .backgroundColor(this.threeButtonColor)
197e41f4b71Sopenharmony_ci        .fontColor(Color.Black)
198e41f4b71Sopenharmony_ci          // Listen for the focus obtaining event of the third component and change its color when it obtains focus.
199e41f4b71Sopenharmony_ci        .onFocus(() => {
200e41f4b71Sopenharmony_ci          this.threeButtonColor = Color.Green;
201e41f4b71Sopenharmony_ci        })
202e41f4b71Sopenharmony_ci          // Listen for the focus loss event of the third component and change its color when it loses focus.
203e41f4b71Sopenharmony_ci        .onBlur(() => {
204e41f4b71Sopenharmony_ci          this.threeButtonColor = Color.Gray ;
205e41f4b71Sopenharmony_ci        })
206e41f4b71Sopenharmony_ci    }.width('100%').margin({ top: 20 })
207e41f4b71Sopenharmony_ci  }
208e41f4b71Sopenharmony_ci}
209e41f4b71Sopenharmony_ci```
210e41f4b71Sopenharmony_ci
211e41f4b71Sopenharmony_ci
212e41f4b71Sopenharmony_ci![en-us_image_0000001511740584](figures/en-us_image_0000001511740584.gif)
213e41f4b71Sopenharmony_ci
214e41f4b71Sopenharmony_ci
215e41f4b71Sopenharmony_ciThe preceding example includes three steps:
216e41f4b71Sopenharmony_ci
217e41f4b71Sopenharmony_ci- When the application is opened, pressing the **Tab** key activates focus traversal, **First Button** displays a focus state style – a blue bounding box around the component – and its **onFocus** callback is triggered, changing the background color to green.
218e41f4b71Sopenharmony_ci- When the **Tab** key is pressed again, **Second Button** gains focus, triggering its **onFocus** callbacktriggered, and its background color turns green, while **First Button** loses focus, triggering its **onBlur** callback, and its background color reverts to gray.
219e41f4b71Sopenharmony_ci- A subsequent **Tab** key press causes **Third Button** to gain focus, triggering its **onFocus** callback, and its background color turns green. Concurrently, **Second Button** loses focus, triggering its **onBlur** callback, and its background color reverts to gray.
220e41f4b71Sopenharmony_ci
221e41f4b71Sopenharmony_ci## Setting Whether a Component Is Focusable
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci```ts
224e41f4b71Sopenharmony_cifocusable(value: boolean)
225e41f4b71Sopenharmony_ci```
226e41f4b71Sopenharmony_ci
227e41f4b71Sopenharmony_ciSets whether the component is focusable.
228e41f4b71Sopenharmony_ci
229e41f4b71Sopenharmony_ciComponents can be classified into the following types based on their focusability:
230e41f4b71Sopenharmony_ci
231e41f4b71Sopenharmony_ci- Default focusable components: These components are usually interactive components, such as **Button**, **Checkbox**, and **TextInput**.
232e41f4b71Sopenharmony_ci
233e41f4b71Sopenharmony_ci- Focusable but default unfocusable components: Typical examples are **Text** and **Image**. To enable them to be focusable, use the **focusable(true)** attribute. When these components do not have the **focusable** attribute set, setting an **onClick** event or a single-tap gesture implicitly makes them focusable. However, when these components have the **focusable** attribute set to **false**, they remain unfocusable even if you bind the aforementioned event or gesture to them.
234e41f4b71Sopenharmony_ci
235e41f4b71Sopenharmony_ci- Non-focusable components: Components that do not allow for interactions, such as **Blank** and **Circle**, cannot be made focusable, even with the **focusable** attribute applied.
236e41f4b71Sopenharmony_ci
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ci
239e41f4b71Sopenharmony_ci
240e41f4b71Sopenharmony_ci```ts
241e41f4b71Sopenharmony_cienabled(value: boolean)
242e41f4b71Sopenharmony_ci```
243e41f4b71Sopenharmony_ci
244e41f4b71Sopenharmony_ciSets the component's interactivity. If [enabled](../reference/apis-arkui/arkui-ts/ts-universal-attributes-enable.md#enabled) is set to **false**, the component becomes non-interactive and cannot gain focus.
245e41f4b71Sopenharmony_ci
246e41f4b71Sopenharmony_ci
247e41f4b71Sopenharmony_ci```ts
248e41f4b71Sopenharmony_civisibility(value: Visibility)
249e41f4b71Sopenharmony_ci```
250e41f4b71Sopenharmony_ci
251e41f4b71Sopenharmony_ciSets the component's visibility. If [visibility](../reference/apis-arkui/arkui-ts/ts-universal-attributes-visibility.md#visibility) set to **Visibility.None** or **Visibility.Hidden**, the component becomes invisible and cannot gain focus.
252e41f4b71Sopenharmony_ci
253e41f4b71Sopenharmony_ci
254e41f4b71Sopenharmony_ci```ts
255e41f4b71Sopenharmony_cifocusOnTouch(value: boolean)
256e41f4b71Sopenharmony_ci```
257e41f4b71Sopenharmony_ci
258e41f4b71Sopenharmony_ciSets whether the component is focusable on touch.
259e41f4b71Sopenharmony_ci
260e41f4b71Sopenharmony_ci
261e41f4b71Sopenharmony_ci
262e41f4b71Sopenharmony_ci
263e41f4b71Sopenharmony_ci>**NOTE**
264e41f4b71Sopenharmony_ci>
265e41f4b71Sopenharmony_ci>- When a component that is currently focused has its **focusable** or **enabled** attribute set to **false**, it automatically loses focus. The focus then shifts to another component according to the [Focus Traversal Guidelines](#focus-traversal-guidelines).
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci
268e41f4b71Sopenharmony_ci```ts
269e41f4b71Sopenharmony_ci// xxx.ets
270e41f4b71Sopenharmony_ci@Entry
271e41f4b71Sopenharmony_ci@Component
272e41f4b71Sopenharmony_cistruct FocusableExample {
273e41f4b71Sopenharmony_ci  @State textFocusable: boolean = true;
274e41f4b71Sopenharmony_ci  @State textEnabled: boolean = true;
275e41f4b71Sopenharmony_ci  @State color1: Color = Color.Yellow;
276e41f4b71Sopenharmony_ci  @State color2: Color = Color.Yellow;
277e41f4b71Sopenharmony_ci  @State color3: Color = Color.Yellow;
278e41f4b71Sopenharmony_ci
279e41f4b71Sopenharmony_ci  build() {
280e41f4b71Sopenharmony_ci    Column({ space: 5 }) {
281e41f4b71Sopenharmony_ci      Text('Default Text')    // The first Text component does not have the focusable attribute set, and is not focusable by default.
282e41f4b71Sopenharmony_ci        .borderColor(this.color1)
283e41f4b71Sopenharmony_ci        .borderWidth(2)
284e41f4b71Sopenharmony_ci        .width(300)
285e41f4b71Sopenharmony_ci        .height(70)
286e41f4b71Sopenharmony_ci        .onFocus(() => {
287e41f4b71Sopenharmony_ci          this.color1 = Color.Blue;
288e41f4b71Sopenharmony_ci        })
289e41f4b71Sopenharmony_ci        .onBlur(() => {
290e41f4b71Sopenharmony_ci          this.color1 = Color.Yellow;
291e41f4b71Sopenharmony_ci        })
292e41f4b71Sopenharmony_ci      Divider()
293e41f4b71Sopenharmony_ci
294e41f4b71Sopenharmony_ci      Text('focusable: ' + this.textFocusable)    // The second Text component initially has focusable set to true and focusOnTouch true.
295e41f4b71Sopenharmony_ci        .borderColor(this.color2)
296e41f4b71Sopenharmony_ci        .borderWidth(2)
297e41f4b71Sopenharmony_ci        .width(300)
298e41f4b71Sopenharmony_ci        .height(70)
299e41f4b71Sopenharmony_ci        .focusable(this.textFocusable)
300e41f4b71Sopenharmony_ci        .focusOnTouch(true)
301e41f4b71Sopenharmony_ci        .onFocus(() => {
302e41f4b71Sopenharmony_ci          this.color2 = Color.Blue;
303e41f4b71Sopenharmony_ci        })
304e41f4b71Sopenharmony_ci        .onBlur(() => {
305e41f4b71Sopenharmony_ci          this.color2 = Color.Yellow;
306e41f4b71Sopenharmony_ci        })
307e41f4b71Sopenharmony_ci
308e41f4b71Sopenharmony_ci      Text('enabled: ' + this.textEnabled)    // The third Text component has focusable set to true, enabled initially true.
309e41f4b71Sopenharmony_ci        .borderColor(this.color3)
310e41f4b71Sopenharmony_ci        .borderWidth(2)
311e41f4b71Sopenharmony_ci        .width(300)
312e41f4b71Sopenharmony_ci        .height(70)
313e41f4b71Sopenharmony_ci        .focusable(true)
314e41f4b71Sopenharmony_ci        .enabled(this.textEnabled)
315e41f4b71Sopenharmony_ci        .focusOnTouch(true)
316e41f4b71Sopenharmony_ci        .onFocus(() => {
317e41f4b71Sopenharmony_ci          this.color3 = Color.Blue;
318e41f4b71Sopenharmony_ci        })
319e41f4b71Sopenharmony_ci        .onBlur(() => {
320e41f4b71Sopenharmony_ci          this.color3 = Color.Yellow;
321e41f4b71Sopenharmony_ci        })
322e41f4b71Sopenharmony_ci
323e41f4b71Sopenharmony_ci      Divider()
324e41f4b71Sopenharmony_ci
325e41f4b71Sopenharmony_ci      Row() {
326e41f4b71Sopenharmony_ci        Button('Button1')
327e41f4b71Sopenharmony_ci          .width(140).height(70)
328e41f4b71Sopenharmony_ci        Button('Button2')
329e41f4b71Sopenharmony_ci          .width(160).height(70)
330e41f4b71Sopenharmony_ci      }
331e41f4b71Sopenharmony_ci
332e41f4b71Sopenharmony_ci      Divider()
333e41f4b71Sopenharmony_ci      Button('Button3')
334e41f4b71Sopenharmony_ci        .width(300).height(70)
335e41f4b71Sopenharmony_ci
336e41f4b71Sopenharmony_ci      Divider()
337e41f4b71Sopenharmony_ci    }.width('100%').justifyContent(FlexAlign.Center)
338e41f4b71Sopenharmony_ci    .onKeyEvent((e) => {
339e41f4b71Sopenharmony_ci      // Bind onKeyEvent. When this Column component has focus, pressing F will toggle the focusable state of the second Text component.
340e41f4b71Sopenharmony_ci      if (e.keyCode === 2022 && e.type === KeyType.Down) {
341e41f4b71Sopenharmony_ci        this.textFocusable = !this.textFocusable;
342e41f4b71Sopenharmony_ci      }
343e41f4b71Sopenharmony_ci      // Bind onKeyEvent. When this Column component has focus, pressing G will toggle the enabled state of the third Text component.
344e41f4b71Sopenharmony_ci      if (e.keyCode === 2023 && e.type === KeyType.Down) {
345e41f4b71Sopenharmony_ci        this.textEnabled = !this.textEnabled;
346e41f4b71Sopenharmony_ci      }
347e41f4b71Sopenharmony_ci    })
348e41f4b71Sopenharmony_ci  }
349e41f4b71Sopenharmony_ci}
350e41f4b71Sopenharmony_ci```
351e41f4b71Sopenharmony_ci
352e41f4b71Sopenharmony_ci
353e41f4b71Sopenharmony_ciOperation result:
354e41f4b71Sopenharmony_ci
355e41f4b71Sopenharmony_ci
356e41f4b71Sopenharmony_ci![focus-1.gif](figures/focus-1.gif)
357e41f4b71Sopenharmony_ci
358e41f4b71Sopenharmony_ciThe preceding example includes three steps:
359e41f4b71Sopenharmony_ci
360e41f4b71Sopenharmony_ci
361e41f4b71Sopenharmony_ci- As the first **Text** component does not have **focusable(true)** set, it is unfocusable.
362e41f4b71Sopenharmony_ci- The second **Text** component is set with **focusOnTouch(true)**, allowing it to gain focus on touch. Pressing the **Tab** key triggers focus traversal, but the focus remains on the second component. When the **F** key is pressed, the **onKeyEvent** callback toggles **focusable** to **false**, making the second **Text** component unfocusable, and the focus shifts to the next available focusable component, which is the third **Text** component.
363e41f4b71Sopenharmony_ci- Pressing the **G** key triggers the **onKeyEvent** callback, which sets **enabled** to **false**, making the third **Text** component unfocusable. The focus then automatically moves to the **Row** container, where the default configuration causes the focus to shift to **Button1**.
364e41f4b71Sopenharmony_ci
365e41f4b71Sopenharmony_ci## Default Focus
366e41f4b71Sopenharmony_ci
367e41f4b71Sopenharmony_ci### Default Focus on a Page
368e41f4b71Sopenharmony_ci
369e41f4b71Sopenharmony_ci```ts
370e41f4b71Sopenharmony_cidefaultFocus(value: boolean)
371e41f4b71Sopenharmony_ci```
372e41f4b71Sopenharmony_ci
373e41f4b71Sopenharmony_ciSpecifies whether to set the component as the default focus of the page.
374e41f4b71Sopenharmony_ci
375e41f4b71Sopenharmony_ci
376e41f4b71Sopenharmony_ci```ts
377e41f4b71Sopenharmony_ci// xxx.ets
378e41f4b71Sopenharmony_ci@Entry
379e41f4b71Sopenharmony_ci@Component
380e41f4b71Sopenharmony_cistruct morenjiaodian {
381e41f4b71Sopenharmony_ci  @State oneButtonColor: Color = Color.Gray;
382e41f4b71Sopenharmony_ci  @State twoButtonColor: Color = Color.Gray;
383e41f4b71Sopenharmony_ci  @State threeButtonColor: Color = Color.Gray;
384e41f4b71Sopenharmony_ci
385e41f4b71Sopenharmony_ci  build() {
386e41f4b71Sopenharmony_ci    Column({ space: 20 }) {
387e41f4b71Sopenharmony_ci      // You can use the up and down arrow keys on an external keyboard to move the focus between the three buttons. When a button gains focus, its color changes. When it loses focus, its color changes back.
388e41f4b71Sopenharmony_ci      Button('First Button')
389e41f4b71Sopenharmony_ci        .width(260)
390e41f4b71Sopenharmony_ci        .height(70)
391e41f4b71Sopenharmony_ci        .backgroundColor(this.oneButtonColor)
392e41f4b71Sopenharmony_ci        .fontColor(Color.Black)
393e41f4b71Sopenharmony_ci          // Listen for the focus obtaining event of the first component and change its color when it obtains focus.
394e41f4b71Sopenharmony_ci        .onFocus(() => {
395e41f4b71Sopenharmony_ci          this.oneButtonColor = Color.Green;
396e41f4b71Sopenharmony_ci        })
397e41f4b71Sopenharmony_ci          // Listen for the focus loss event of the first component and change its color when it loses focus.
398e41f4b71Sopenharmony_ci        .onBlur(() => {
399e41f4b71Sopenharmony_ci          this.oneButtonColor = Color.Gray;
400e41f4b71Sopenharmony_ci        })
401e41f4b71Sopenharmony_ci
402e41f4b71Sopenharmony_ci      Button('Second Button')
403e41f4b71Sopenharmony_ci        .width(260)
404e41f4b71Sopenharmony_ci        .height(70)
405e41f4b71Sopenharmony_ci        .backgroundColor(this.twoButtonColor)
406e41f4b71Sopenharmony_ci        .fontColor(Color.Black)
407e41f4b71Sopenharmony_ci          // Listen for the focus obtaining event of the second component and change its color when it obtains focus.
408e41f4b71Sopenharmony_ci        .onFocus(() => {
409e41f4b71Sopenharmony_ci          this.twoButtonColor = Color.Green;
410e41f4b71Sopenharmony_ci        })
411e41f4b71Sopenharmony_ci          // Listen for the focus loss event of the second component and change its color when it loses focus.
412e41f4b71Sopenharmony_ci        .onBlur(() => {
413e41f4b71Sopenharmony_ci          this.twoButtonColor = Color.Grey;
414e41f4b71Sopenharmony_ci        })
415e41f4b71Sopenharmony_ci
416e41f4b71Sopenharmony_ci      Button('Third Button')
417e41f4b71Sopenharmony_ci        .width(260)
418e41f4b71Sopenharmony_ci        .height(70)
419e41f4b71Sopenharmony_ci        .backgroundColor(this.threeButtonColor)
420e41f4b71Sopenharmony_ci        .fontColor(Color.Black)
421e41f4b71Sopenharmony_ci          // Set the default focus.
422e41f4b71Sopenharmony_ci        .defaultFocus(true)
423e41f4b71Sopenharmony_ci          // Listen for the focus obtaining event of the third component and change its color when it obtains focus.
424e41f4b71Sopenharmony_ci        .onFocus(() => {
425e41f4b71Sopenharmony_ci          this.threeButtonColor = Color.Green;
426e41f4b71Sopenharmony_ci        })
427e41f4b71Sopenharmony_ci          // Listen for the focus loss event of the third component and change its color when it loses focus.
428e41f4b71Sopenharmony_ci        .onBlur(() => {
429e41f4b71Sopenharmony_ci          this.threeButtonColor = Color.Gray ;
430e41f4b71Sopenharmony_ci        })
431e41f4b71Sopenharmony_ci    }.width('100%').margin({ top: 20 })
432e41f4b71Sopenharmony_ci  }
433e41f4b71Sopenharmony_ci}
434e41f4b71Sopenharmony_ci```
435e41f4b71Sopenharmony_ci
436e41f4b71Sopenharmony_ci![defaultFocus.gif](figures/defaultFocus.gif)
437e41f4b71Sopenharmony_ci
438e41f4b71Sopenharmony_ciThe preceding example includes two steps:
439e41f4b71Sopenharmony_ci
440e41f4b71Sopenharmony_ci- The **defaultFocus(true)** is set on the third **Button** component, which means it gains focus by default when the page is loaded, displaying in green.
441e41f4b71Sopenharmony_ci- Pressing the **Tab** key triggers focus traversal, and since the third **Button** component is in focus, a focus frame appears around it.
442e41f4b71Sopenharmony_ci
443e41f4b71Sopenharmony_ci### Default Focus for Containers
444e41f4b71Sopenharmony_ci
445e41f4b71Sopenharmony_ciThe default focus within a container is affected by [focus priority](#focus-group-and-focus-priority).
446e41f4b71Sopenharmony_ci
447e41f4b71Sopenharmony_ci**Differences Between defaultFocus and FocusPriority**
448e41f4b71Sopenharmony_ci
449e41f4b71Sopenharmony_ci[defaultFocus](../reference/apis-arkui/arkui-ts/ts-universal-attributes-focus.md#defaultfocus9) specifies the initial focus when the page loads. [FocusPriority](../reference/apis-arkui/arkui-ts/ts-universal-attributes-focus.md#focuspriority12) defines the order in which child components gain focus within a container. Behavior is undefined when both attributes are set in some scenarios. For example, a page's initial display cannot simultaneously meet the focus requirements of a component with **defaultFocus **and a high-priority component.
450e41f4b71Sopenharmony_ci
451e41f4b71Sopenharmony_ciExample
452e41f4b71Sopenharmony_ci
453e41f4b71Sopenharmony_ci```ts
454e41f4b71Sopenharmony_ci@Entry
455e41f4b71Sopenharmony_ci@Component
456e41f4b71Sopenharmony_cistruct Index {
457e41f4b71Sopenharmony_ci  build() {
458e41f4b71Sopenharmony_ci    Row() {
459e41f4b71Sopenharmony_ci      Button('Button1')
460e41f4b71Sopenharmony_ci        .defaultFocus(true)
461e41f4b71Sopenharmony_ci      Button('Button2')
462e41f4b71Sopenharmony_ci        .focusScopePriority('RowScope', FocusPriority.PREVIOUS)
463e41f4b71Sopenharmony_ci    }.focusScopeId('RowScope')
464e41f4b71Sopenharmony_ci  }
465e41f4b71Sopenharmony_ci}
466e41f4b71Sopenharmony_ci```
467e41f4b71Sopenharmony_ci
468e41f4b71Sopenharmony_ci### Focus Chain for Pages/Containers
469e41f4b71Sopenharmony_ci
470e41f4b71Sopenharmony_ci**Overall Focus and Non-Overall Focus**
471e41f4b71Sopenharmony_ci
472e41f4b71Sopenharmony_ci- Overall focus: The entire page or container gains focus first, then the focus shifts to its child components. Examples include page transitions, route switches within **Navigation** components, focus group traversal, and when a container component proactively calls **requestFocusById**.
473e41f4b71Sopenharmony_ci
474e41f4b71Sopenharmony_ci- Non-overall focus: A specific component gains focus, pulling its parent components into focus. Examples include a **TextInput** component proactively obtaining focus or using the **Tab** key for traversal in non-focus group.
475e41f4b71Sopenharmony_ci
476e41f4b71Sopenharmony_ci**Formation of the Focus Chain in Overall Focus**
477e41f4b71Sopenharmony_ci
478e41f4b71Sopenharmony_ci1. Initial page focus:
479e41f4b71Sopenharmony_ci
480e41f4b71Sopenharmony_ci- The leaf node of the focus chain is the node with **defaultFocus** set.
481e41f4b71Sopenharmony_ci
482e41f4b71Sopenharmony_ci- If no **defaultFocus** is configured, the focus remains on the page's root container.
483e41f4b71Sopenharmony_ci
484e41f4b71Sopenharmony_ci2. Subsequent page focus: Focus is gained by the node that last held focus.
485e41f4b71Sopenharmony_ci
486e41f4b71Sopenharmony_ci3. Focus chain with priority configuration:
487e41f4b71Sopenharmony_ci
488e41f4b71Sopenharmony_ci- If a container has a component with a focus priority higher than **PREVIOUS**, the component with the highest priority gains focus.
489e41f4b71Sopenharmony_ci
490e41f4b71Sopenharmony_ci- If no component with a priority higher than **PREVIOUS** exists, the last focused node regains focus, such as when a window refocuses after being out of focus.
491e41f4b71Sopenharmony_ci
492e41f4b71Sopenharmony_ci
493e41f4b71Sopenharmony_ci## Focus Style
494e41f4b71Sopenharmony_ci
495e41f4b71Sopenharmony_ci
496e41f4b71Sopenharmony_ci```ts
497e41f4b71Sopenharmony_cifocusBox(style: FocusBoxStyle)
498e41f4b71Sopenharmony_ci```
499e41f4b71Sopenharmony_ci
500e41f4b71Sopenharmony_ciSets the system focus box style for the component.
501e41f4b71Sopenharmony_ci
502e41f4b71Sopenharmony_ci```ts
503e41f4b71Sopenharmony_ciimport { ColorMetrics, LengthMetrics } from '@kit.ArkUI'
504e41f4b71Sopenharmony_ci
505e41f4b71Sopenharmony_ci@Entry
506e41f4b71Sopenharmony_ci@Component
507e41f4b71Sopenharmony_cistruct RequestFocusExample {
508e41f4b71Sopenharmony_ci  build() {
509e41f4b71Sopenharmony_ci    Column({ space: 30 }) {
510e41f4b71Sopenharmony_ci      Button("small black focus box")
511e41f4b71Sopenharmony_ci        .focusBox({
512e41f4b71Sopenharmony_ci          margin: new LengthMetrics(0),
513e41f4b71Sopenharmony_ci          strokeColor: ColorMetrics.rgba(0, 0, 0),
514e41f4b71Sopenharmony_ci        })
515e41f4b71Sopenharmony_ci      Button("large red focus box")
516e41f4b71Sopenharmony_ci        .focusBox({
517e41f4b71Sopenharmony_ci          margin: LengthMetrics.px(20),
518e41f4b71Sopenharmony_ci          strokeColor: ColorMetrics.rgba(255, 0, 0),
519e41f4b71Sopenharmony_ci          strokeWidth: LengthMetrics.px(10)
520e41f4b71Sopenharmony_ci        })
521e41f4b71Sopenharmony_ci    }
522e41f4b71Sopenharmony_ci    .alignItems(HorizontalAlign.Center)
523e41f4b71Sopenharmony_ci    .width('100%')
524e41f4b71Sopenharmony_ci  }
525e41f4b71Sopenharmony_ci}
526e41f4b71Sopenharmony_ci```
527e41f4b71Sopenharmony_ci
528e41f4b71Sopenharmony_ci![focusBox](figures/focusBox.gif)
529e41f4b71Sopenharmony_ci
530e41f4b71Sopenharmony_ci
531e41f4b71Sopenharmony_ciThe preceding example includes two steps:
532e41f4b71Sopenharmony_ci
533e41f4b71Sopenharmony_ci- After the page opens, pressing the Tab key initiates focus traversal. The first **Button** gains focus, displaying a small, black focus box that is closely fitted to the edge.
534e41f4b71Sopenharmony_ci- Pressing the Tab key again shifts focus to the second **Button**, which features a large, red focus box with a thicker stroke and a more significant margin from the edge.
535e41f4b71Sopenharmony_ci
536e41f4b71Sopenharmony_ci## Active Focus Acquisition/Loss
537e41f4b71Sopenharmony_ci
538e41f4b71Sopenharmony_ci- Using **FocusController** APIs
539e41f4b71Sopenharmony_ci
540e41f4b71Sopenharmony_ci  You are advised to use **requestFocus** from **FocusController** for actively acquiring focus. It provides the following benefits:
541e41f4b71Sopenharmony_ci  - Takes effect in the current frame, preventing interference from subsequent component tree changes.
542e41f4b71Sopenharmony_ci  - Provides exception handling, aiding in troubleshooting focus acquisition issues.
543e41f4b71Sopenharmony_ci  - Prevents errors in multi-instance scenarios by avoiding incorrect instance retrieval.
544e41f4b71Sopenharmony_ci
545e41f4b71Sopenharmony_ci  You must first obtain an instance using the [getFocusController()](../reference/apis-arkui/js-apis-arkui-UIContext.md#getfocuscontroller12) API in **UIContext** and then use this instance to call the corresponding methods.
546e41f4b71Sopenharmony_ci
547e41f4b71Sopenharmony_ci  ```ts
548e41f4b71Sopenharmony_ci  requestFocus(key: string): void
549e41f4b71Sopenharmony_ci  ```
550e41f4b71Sopenharmony_ci  Transfers focus to a component node by the component ID, which is effective immediately.
551e41f4b71Sopenharmony_ci
552e41f4b71Sopenharmony_ci  ```ts
553e41f4b71Sopenharmony_ci  clearFocus(): void
554e41f4b71Sopenharmony_ci  ```
555e41f4b71Sopenharmony_ci  Clears the focus and forcibly moves the focus to the root container node of the page, causing other nodes in the focus chain to lose focus.
556e41f4b71Sopenharmony_ci
557e41f4b71Sopenharmony_ci- Using **focusControl** APIs
558e41f4b71Sopenharmony_ci  ```ts
559e41f4b71Sopenharmony_ci  requestFocus(value: string): boolean
560e41f4b71Sopenharmony_ci  ```
561e41f4b71Sopenharmony_ci
562e41f4b71Sopenharmony_ci  Moves focus to a specified component, with the change taking effect in the next frame.
563e41f4b71Sopenharmony_ci
564e41f4b71Sopenharmony_ci
565e41f4b71Sopenharmony_ci```ts
566e41f4b71Sopenharmony_ci// focusTest.ets
567e41f4b71Sopenharmony_ci@Entry
568e41f4b71Sopenharmony_ci@Component
569e41f4b71Sopenharmony_cistruct RequestExample {
570e41f4b71Sopenharmony_ci  @State btColor: string = '#ff2787d9'
571e41f4b71Sopenharmony_ci  @State btColor2: string = '#ff2787d9'
572e41f4b71Sopenharmony_ci
573e41f4b71Sopenharmony_ci  build() {
574e41f4b71Sopenharmony_ci    Column({ space: 20 }) {
575e41f4b71Sopenharmony_ci      Column({ space: 5 }) {
576e41f4b71Sopenharmony_ci        Button('Button')
577e41f4b71Sopenharmony_ci          .width(200)
578e41f4b71Sopenharmony_ci          .height(70)
579e41f4b71Sopenharmony_ci          .fontColor(Color.White)
580e41f4b71Sopenharmony_ci          .focusOnTouch(true)
581e41f4b71Sopenharmony_ci          .backgroundColor(this.btColor)
582e41f4b71Sopenharmony_ci          .onFocus(() => {
583e41f4b71Sopenharmony_ci            this.btColor = '#ffd5d5d5'
584e41f4b71Sopenharmony_ci          })
585e41f4b71Sopenharmony_ci          .onBlur(() => {
586e41f4b71Sopenharmony_ci            this.btColor = '#ff2787d9'
587e41f4b71Sopenharmony_ci          })
588e41f4b71Sopenharmony_ci          .id("testButton")
589e41f4b71Sopenharmony_ci
590e41f4b71Sopenharmony_ci        Button('Button')
591e41f4b71Sopenharmony_ci          .width(200)
592e41f4b71Sopenharmony_ci          .height(70)
593e41f4b71Sopenharmony_ci          .fontColor(Color.White)
594e41f4b71Sopenharmony_ci          .focusOnTouch(true)
595e41f4b71Sopenharmony_ci          .backgroundColor(this.btColor2)
596e41f4b71Sopenharmony_ci          .onFocus(() => {
597e41f4b71Sopenharmony_ci            this.btColor2 = '#ffd5d5d5'
598e41f4b71Sopenharmony_ci          })
599e41f4b71Sopenharmony_ci          .onBlur(() => {
600e41f4b71Sopenharmony_ci            this.btColor2 = '#ff2787d9'
601e41f4b71Sopenharmony_ci          })
602e41f4b71Sopenharmony_ci          .id("testButton2")
603e41f4b71Sopenharmony_ci
604e41f4b71Sopenharmony_ci        Divider()
605e41f4b71Sopenharmony_ci          .vertical(false)
606e41f4b71Sopenharmony_ci          .width("80%")
607e41f4b71Sopenharmony_ci          .backgroundColor('#ff707070')
608e41f4b71Sopenharmony_ci          .height(10)
609e41f4b71Sopenharmony_ci
610e41f4b71Sopenharmony_ci        Button('FocusController.requestFocus')
611e41f4b71Sopenharmony_ci          .width(200).height(70).fontColor(Color.White)
612e41f4b71Sopenharmony_ci          .onClick(() => {
613e41f4b71Sopenharmony_ci            this.getUIContext().getFocusController().requestFocus("testButton")
614e41f4b71Sopenharmony_ci          })
615e41f4b71Sopenharmony_ci          .backgroundColor('#ff2787d9')
616e41f4b71Sopenharmony_ci
617e41f4b71Sopenharmony_ci        Button("focusControl.requestFocus")
618e41f4b71Sopenharmony_ci          .width(200).height(70).fontColor(Color.White)
619e41f4b71Sopenharmony_ci          .onClick(() => {
620e41f4b71Sopenharmony_ci            focusControl.requestFocus("testButton2")
621e41f4b71Sopenharmony_ci          })
622e41f4b71Sopenharmony_ci          .backgroundColor('#ff2787d9')
623e41f4b71Sopenharmony_ci
624e41f4b71Sopenharmony_ci        Button("clearFocus")
625e41f4b71Sopenharmony_ci          .width(200).height(70).fontColor(Color.White)
626e41f4b71Sopenharmony_ci          .onClick(() => {
627e41f4b71Sopenharmony_ci            this.getUIContext().getFocusController().clearFocus()
628e41f4b71Sopenharmony_ci          })
629e41f4b71Sopenharmony_ci          .backgroundColor('#ff2787d9')
630e41f4b71Sopenharmony_ci      }
631e41f4b71Sopenharmony_ci    }
632e41f4b71Sopenharmony_ci    .width('100%')
633e41f4b71Sopenharmony_ci    .height('100%')
634e41f4b71Sopenharmony_ci  }
635e41f4b71Sopenharmony_ci}
636e41f4b71Sopenharmony_ci```
637e41f4b71Sopenharmony_ci
638e41f4b71Sopenharmony_ci![focus-2](figures/focus-2.gif)
639e41f4b71Sopenharmony_ci
640e41f4b71Sopenharmony_ciThe preceding example includes three steps:
641e41f4b71Sopenharmony_ci
642e41f4b71Sopenharmony_ci- When the **FocusController.requestFocus** button is clicked, the first button gains focus.
643e41f4b71Sopenharmony_ci- When the **focusControl.requestFocus** button is clicked, the second button gains focus.
644e41f4b71Sopenharmony_ci- When the **clearFocus** button is clicked, the second button loses focus.
645e41f4b71Sopenharmony_ci
646e41f4b71Sopenharmony_ci## Focus Group and Focus Priority
647e41f4b71Sopenharmony_ci
648e41f4b71Sopenharmony_ci```ts
649e41f4b71Sopenharmony_cifocusScopePriority(scopeId: string, priority?: FocusPriority)
650e41f4b71Sopenharmony_ci```
651e41f4b71Sopenharmony_ci
652e41f4b71Sopenharmony_ciSets the focus priority of this component in a specified container. It must be used together with **focusScopeId**.
653e41f4b71Sopenharmony_ci
654e41f4b71Sopenharmony_ci
655e41f4b71Sopenharmony_ci```ts
656e41f4b71Sopenharmony_cifocusScopeId(id: string, isGroup?: boolean)
657e41f4b71Sopenharmony_ci```
658e41f4b71Sopenharmony_ci
659e41f4b71Sopenharmony_ciAssigns an ID to this container component and specifies whether the container is a focus group. Focus groups should not be mixed with **tabIndex** usage.
660e41f4b71Sopenharmony_ci
661e41f4b71Sopenharmony_ci```ts
662e41f4b71Sopenharmony_ci// focusTest.ets
663e41f4b71Sopenharmony_ci@Entry
664e41f4b71Sopenharmony_ci@Component
665e41f4b71Sopenharmony_cistruct FocusableExample {
666e41f4b71Sopenharmony_ci  @State inputValue: string = ''
667e41f4b71Sopenharmony_ci
668e41f4b71Sopenharmony_ci  build() {
669e41f4b71Sopenharmony_ci    Scroll() {
670e41f4b71Sopenharmony_ci      Row({ space: 20 }) {
671e41f4b71Sopenharmony_ci        Column({ space: 20 }) {  // Labeled as Column1.
672e41f4b71Sopenharmony_ci          Column({ space: 5 }) {
673e41f4b71Sopenharmony_ci            Button('Group1')
674e41f4b71Sopenharmony_ci              .width(165)
675e41f4b71Sopenharmony_ci              .height(40)
676e41f4b71Sopenharmony_ci              .fontColor(Color.White)
677e41f4b71Sopenharmony_ci            Row({ space: 5 }) {
678e41f4b71Sopenharmony_ci              Button()
679e41f4b71Sopenharmony_ci                .width(80)
680e41f4b71Sopenharmony_ci                .height(40)
681e41f4b71Sopenharmony_ci                .fontColor(Color.White)
682e41f4b71Sopenharmony_ci              Button()
683e41f4b71Sopenharmony_ci                .width(80)
684e41f4b71Sopenharmony_ci                .height(40)
685e41f4b71Sopenharmony_ci                .fontColor(Color.White)
686e41f4b71Sopenharmony_ci            }
687e41f4b71Sopenharmony_ci            Row({ space: 5 }) {
688e41f4b71Sopenharmony_ci              Button()
689e41f4b71Sopenharmony_ci                .width(80)
690e41f4b71Sopenharmony_ci                .height(40)
691e41f4b71Sopenharmony_ci                .fontColor(Color.White)
692e41f4b71Sopenharmony_ci              Button()
693e41f4b71Sopenharmony_ci                .width(80)
694e41f4b71Sopenharmony_ci                .height(40)
695e41f4b71Sopenharmony_ci                .fontColor(Color.White)
696e41f4b71Sopenharmony_ci            }
697e41f4b71Sopenharmony_ci          }.borderWidth(2).borderColor(Color.Red).borderStyle(BorderStyle.Dashed)
698e41f4b71Sopenharmony_ci          Column({ space: 5 }) {
699e41f4b71Sopenharmony_ci            Button('Group2')
700e41f4b71Sopenharmony_ci              .width(165)
701e41f4b71Sopenharmony_ci              .height(40)
702e41f4b71Sopenharmony_ci              .fontColor(Color.White)
703e41f4b71Sopenharmony_ci            Row({ space: 5 }) {
704e41f4b71Sopenharmony_ci              Button()
705e41f4b71Sopenharmony_ci                .width(80)
706e41f4b71Sopenharmony_ci                .height(40)
707e41f4b71Sopenharmony_ci                .fontColor(Color.White)
708e41f4b71Sopenharmony_ci              Button()
709e41f4b71Sopenharmony_ci                .width(80)
710e41f4b71Sopenharmony_ci                .height(40)
711e41f4b71Sopenharmony_ci                .fontColor(Color.White)
712e41f4b71Sopenharmony_ci                .focusScopePriority('ColumnScope1', FocusPriority.PRIOR) // Focuses when Column1 first gains focus.
713e41f4b71Sopenharmony_ci            }
714e41f4b71Sopenharmony_ci            Row({ space: 5 }) {
715e41f4b71Sopenharmony_ci              Button()
716e41f4b71Sopenharmony_ci                .width(80)
717e41f4b71Sopenharmony_ci                .height(40)
718e41f4b71Sopenharmony_ci                .fontColor(Color.White)
719e41f4b71Sopenharmony_ci              Button()
720e41f4b71Sopenharmony_ci                .width(80)
721e41f4b71Sopenharmony_ci                .height(40)
722e41f4b71Sopenharmony_ci                .fontColor(Color.White)
723e41f4b71Sopenharmony_ci            }
724e41f4b71Sopenharmony_ci          }.borderWidth(2).borderColor(Color.Green).borderStyle(BorderStyle.Dashed)
725e41f4b71Sopenharmony_ci        }
726e41f4b71Sopenharmony_ci        .focusScopeId('ColumnScope1')
727e41f4b71Sopenharmony_ci        Column({ space: 5 }) {  // Labeled as Column2.
728e41f4b71Sopenharmony_ci          TextInput({placeholder: 'input', text: this.inputValue})
729e41f4b71Sopenharmony_ci            .onChange((value: string) => {
730e41f4b71Sopenharmony_ci              this.inputValue = value
731e41f4b71Sopenharmony_ci            })
732e41f4b71Sopenharmony_ci            .width(156)
733e41f4b71Sopenharmony_ci          Button('Group3')
734e41f4b71Sopenharmony_ci            .width(165)
735e41f4b71Sopenharmony_ci            .height(40)
736e41f4b71Sopenharmony_ci            .fontColor(Color.White)
737e41f4b71Sopenharmony_ci          Row({ space: 5 }) {
738e41f4b71Sopenharmony_ci            Button()
739e41f4b71Sopenharmony_ci              .width(80)
740e41f4b71Sopenharmony_ci              .height(40)
741e41f4b71Sopenharmony_ci              .fontColor(Color.White)
742e41f4b71Sopenharmony_ci            Button()
743e41f4b71Sopenharmony_ci              .width(80)
744e41f4b71Sopenharmony_ci              .height(40)
745e41f4b71Sopenharmony_ci              .fontColor(Color.White)
746e41f4b71Sopenharmony_ci          }
747e41f4b71Sopenharmony_ci          Button()
748e41f4b71Sopenharmony_ci            .width(165)
749e41f4b71Sopenharmony_ci            .height(40)
750e41f4b71Sopenharmony_ci            .fontColor(Color.White)
751e41f4b71Sopenharmony_ci            .focusScopePriority('ColumnScope2', FocusPriority.PREVIOUS)  // Focuses when Column2 first gains focus.
752e41f4b71Sopenharmony_ci          Row({ space: 5 }) {
753e41f4b71Sopenharmony_ci            Button()
754e41f4b71Sopenharmony_ci              .width(80)
755e41f4b71Sopenharmony_ci              .height(40)
756e41f4b71Sopenharmony_ci              .fontColor(Color.White)
757e41f4b71Sopenharmony_ci            Button()
758e41f4b71Sopenharmony_ci              .width(80)
759e41f4b71Sopenharmony_ci              .height(40)
760e41f4b71Sopenharmony_ci              .fontColor(Color.White)
761e41f4b71Sopenharmony_ci          }
762e41f4b71Sopenharmony_ci          Button()
763e41f4b71Sopenharmony_ci            .width(165)
764e41f4b71Sopenharmony_ci            .height(40)
765e41f4b71Sopenharmony_ci            .fontColor(Color.White)
766e41f4b71Sopenharmony_ci          Row({ space: 5 }) {
767e41f4b71Sopenharmony_ci            Button()
768e41f4b71Sopenharmony_ci              .width(80)
769e41f4b71Sopenharmony_ci              .height(40)
770e41f4b71Sopenharmony_ci              .fontColor(Color.White)
771e41f4b71Sopenharmony_ci            Button()
772e41f4b71Sopenharmony_ci              .width(80)
773e41f4b71Sopenharmony_ci              .height(40)
774e41f4b71Sopenharmony_ci              .fontColor(Color.White)
775e41f4b71Sopenharmony_ci          }
776e41f4b71Sopenharmony_ci        }.borderWidth(2).borderColor(Color.Orange).borderStyle(BorderStyle.Dashed)
777e41f4b71Sopenharmony_ci        .focusScopeId('ColumnScope2', true) // Column2 is a focus group.
778e41f4b71Sopenharmony_ci      }.alignItems(VerticalAlign.Top)
779e41f4b71Sopenharmony_ci    }
780e41f4b71Sopenharmony_ci  }
781e41f4b71Sopenharmony_ci}
782e41f4b71Sopenharmony_ci```
783e41f4b71Sopenharmony_ci
784e41f4b71Sopenharmony_ci
785e41f4b71Sopenharmony_ci![focus-3](figures/focus-3.gif)
786e41f4b71Sopenharmony_ci
787e41f4b71Sopenharmony_ci
788e41f4b71Sopenharmony_ci
789e41f4b71Sopenharmony_ciThe preceding example includes two steps:
790e41f4b71Sopenharmony_ci
791e41f4b71Sopenharmony_ci- The input box is set with a focus group, which means that when the **Tab** key is pressed, the focus quickly moves out from the input, and when arrow keys are used, the focus stays within the input.
792e41f4b71Sopenharmony_ci- The **Column** component in the upper left corner does not have a focus group set. Therefore, focus can only be traversed one by one with the **Tab** key.
793e41f4b71Sopenharmony_ci
794e41f4b71Sopenharmony_ci## Component Focusability
795e41f4b71Sopenharmony_ci
796e41f4b71Sopenharmony_ci
797e41f4b71Sopenharmony_ci  **Table 1** Focusability of basic components
798e41f4b71Sopenharmony_ci
799e41f4b71Sopenharmony_ci| Basic Component                                    | Focusable| Default Value of focusable|
800e41f4b71Sopenharmony_ci| ---------------------------------------- | ------- | ------------ |
801e41f4b71Sopenharmony_ci| [AlphabetIndexer](../reference/apis-arkui/arkui-ts/ts-container-alphabet-indexer.md) | Yes      | true         |
802e41f4b71Sopenharmony_ci| [Blank](../reference/apis-arkui/arkui-ts/ts-basic-components-blank.md) | No      | false        |
803e41f4b71Sopenharmony_ci| [Button](../reference/apis-arkui/arkui-ts/ts-basic-components-button.md) | Yes      | true         |
804e41f4b71Sopenharmony_ci| [CalendarPicker](../reference/apis-arkui/arkui-ts/ts-basic-components-calendarpicker.md) | Yes      | true         |
805e41f4b71Sopenharmony_ci| [Checkbox](../reference/apis-arkui/arkui-ts/ts-basic-components-checkbox.md) | Yes      | true         |
806e41f4b71Sopenharmony_ci| [CheckboxGroup](../reference/apis-arkui/arkui-ts/ts-basic-components-checkboxgroup.md) | Yes      | true         |
807e41f4b71Sopenharmony_ci| [ContainerSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-containerspan.md) | No      | false         |
808e41f4b71Sopenharmony_ci| [DataPanel](../reference/apis-arkui/arkui-ts/ts-basic-components-datapanel.md) | Yes      | false        |
809e41f4b71Sopenharmony_ci| [DatePicker](../reference/apis-arkui/arkui-ts/ts-basic-components-datepicker.md) | Yes      | true         |
810e41f4b71Sopenharmony_ci| [Divider](../reference/apis-arkui/arkui-ts/ts-basic-components-divider.md) | Yes      | false        |
811e41f4b71Sopenharmony_ci| [Gauge](../reference/apis-arkui/arkui-ts/ts-basic-components-gauge.md) | Yes      | false        |
812e41f4b71Sopenharmony_ci| [Image](../reference/apis-arkui/arkui-ts/ts-basic-components-image.md) | Yes      | false        |
813e41f4b71Sopenharmony_ci| [ImageAnimator](../reference/apis-arkui/arkui-ts/ts-basic-components-imageanimator.md) | No      | false        |
814e41f4b71Sopenharmony_ci| [ImageSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-imagespan.md)                 | No      | false        |
815e41f4b71Sopenharmony_ci| [LoadingProgress](../reference/apis-arkui/arkui-ts/ts-basic-components-loadingprogress.md) | Yes      | true        |
816e41f4b71Sopenharmony_ci| [Marquee](../reference/apis-arkui/arkui-ts/ts-basic-components-marquee.md) | No      | false        |
817e41f4b71Sopenharmony_ci| [Menu](../reference/apis-arkui/arkui-ts/ts-basic-components-menu.md) | Yes      | true         |
818e41f4b71Sopenharmony_ci| [MenuItem](../reference/apis-arkui/arkui-ts/ts-basic-components-menuitem.md) | Yes      | true         |
819e41f4b71Sopenharmony_ci| [MenuItemGroup](../reference/apis-arkui/arkui-ts/ts-basic-components-menuitemgroup.md) | No      | false         |
820e41f4b71Sopenharmony_ci| [Navigation](../reference/apis-arkui/arkui-ts/ts-basic-components-navigation.md) | Yes      | true       |
821e41f4b71Sopenharmony_ci| [NavRouter](../reference/apis-arkui/arkui-ts/ts-basic-components-navrouter.md) | No      | false        |
822e41f4b71Sopenharmony_ci| [NavDestination](../reference/apis-arkui/arkui-ts/ts-basic-components-navdestination.md) | Yes      | true        |
823e41f4b71Sopenharmony_ci| [PatternLock](../reference/apis-arkui/arkui-ts/ts-basic-components-patternlock.md) | Yes      | true        |
824e41f4b71Sopenharmony_ci| [Progress](../reference/apis-arkui/arkui-ts/ts-basic-components-progress.md) | Yes      | true        |
825e41f4b71Sopenharmony_ci| [QRCode](../reference/apis-arkui/arkui-ts/ts-basic-components-qrcode.md) | Yes      | true        |
826e41f4b71Sopenharmony_ci| [Radio](../reference/apis-arkui/arkui-ts/ts-basic-components-radio.md) | Yes      | true         |
827e41f4b71Sopenharmony_ci| [Rating](../reference/apis-arkui/arkui-ts/ts-basic-components-rating.md) | Yes      | true         |
828e41f4b71Sopenharmony_ci| [RichEditor](../reference/apis-arkui/arkui-ts/ts-basic-components-richeditor.md) | Yes      | true         |
829e41f4b71Sopenharmony_ci| [RichText](../reference/apis-arkui/arkui-ts/ts-basic-components-richtext.md) | No      | false        |
830e41f4b71Sopenharmony_ci| [ScrollBar](../reference/apis-arkui/arkui-ts/ts-basic-components-scrollbar.md) | No      | false        |
831e41f4b71Sopenharmony_ci| [Search](../reference/apis-arkui/arkui-ts/ts-basic-components-search.md) | Yes      | true         |
832e41f4b71Sopenharmony_ci| [Select](../reference/apis-arkui/arkui-ts/ts-basic-components-select.md) | Yes      | true         |
833e41f4b71Sopenharmony_ci| [Slider](../reference/apis-arkui/arkui-ts/ts-basic-components-slider.md) | Yes      | true         |
834e41f4b71Sopenharmony_ci| [Span](../reference/apis-arkui/arkui-ts/ts-basic-components-span.md) | No      | false        |
835e41f4b71Sopenharmony_ci| [Stepper](../reference/apis-arkui/arkui-ts/ts-basic-components-stepper.md) | Yes      | true         |
836e41f4b71Sopenharmony_ci| [StepperItem](../reference/apis-arkui/arkui-ts/ts-basic-components-stepperitem.md) | Yes      | true         |
837e41f4b71Sopenharmony_ci| [SymbolSpan](../reference/apis-arkui/arkui-ts/ts-basic-components-symbolSpan.md) | No      | false         |
838e41f4b71Sopenharmony_ci| [SymbolGlyph](../reference/apis-arkui/arkui-ts/ts-basic-components-symbolGlyph.md) | No      | false         |
839e41f4b71Sopenharmony_ci| [Text](../reference/apis-arkui/arkui-ts/ts-basic-components-text.md) | Yes      | false        |
840e41f4b71Sopenharmony_ci| [TextArea](../reference/apis-arkui/arkui-ts/ts-basic-components-textarea.md) | No      | false         |
841e41f4b71Sopenharmony_ci| [TextClock](../reference/apis-arkui/arkui-ts/ts-basic-components-textclock.md) | No      | false        |
842e41f4b71Sopenharmony_ci| [TextInput](../reference/apis-arkui/arkui-ts/ts-basic-components-textinput.md) | Yes      | true         |
843e41f4b71Sopenharmony_ci| [TextPicker](../reference/apis-arkui/arkui-ts/ts-basic-components-textpicker.md) | Yes      | true         |
844e41f4b71Sopenharmony_ci| [TextTimer](../reference/apis-arkui/arkui-ts/ts-basic-components-texttimer.md) | No      | false        |
845e41f4b71Sopenharmony_ci| [TimePicker](../reference/apis-arkui/arkui-ts/ts-basic-components-timepicker.md) | No      | false         |
846e41f4b71Sopenharmony_ci| [Toggle](../reference/apis-arkui/arkui-ts/ts-basic-components-toggle.md) | Yes      | true         |
847e41f4b71Sopenharmony_ci| [XComponent](../reference/apis-arkui/arkui-ts/ts-basic-components-xcomponent.md) | Yes      | false        |
848e41f4b71Sopenharmony_ci
849e41f4b71Sopenharmony_ci  **Table 2** Focusability of container components
850e41f4b71Sopenharmony_ci
851e41f4b71Sopenharmony_ci| Container Component                                    | Focusable| Default Value of focusable|
852e41f4b71Sopenharmony_ci| ---------------------------------------- | ----- | ------------ |
853e41f4b71Sopenharmony_ci| [Badge](../reference/apis-arkui/arkui-ts/ts-container-badge.md) | No    | false        |
854e41f4b71Sopenharmony_ci| [Column](../reference/apis-arkui/arkui-ts/ts-container-column.md) | Yes    | true         |
855e41f4b71Sopenharmony_ci| [ColumnSplit](../reference/apis-arkui/arkui-ts/ts-container-columnsplit.md) | Yes    | true         |
856e41f4b71Sopenharmony_ci| [Counter](../reference/apis-arkui/arkui-ts/ts-container-counter.md) | Yes    | false         |
857e41f4b71Sopenharmony_ci| [EmbeddedComponent](../reference/apis-arkui/arkui-ts/ts-container-embedded-component.md)    | No    | false         |
858e41f4b71Sopenharmony_ci| [Flex](../reference/apis-arkui/arkui-ts/ts-container-flex.md) | Yes    | true         |
859e41f4b71Sopenharmony_ci| [FlowItem](../reference/apis-arkui/arkui-ts/ts-container-flowitem.md)             | Yes    | true         |
860e41f4b71Sopenharmony_ci| [FolderStack](../reference/apis-arkui/arkui-ts/ts-container-folderstack.md)             | Yes    | true         |
861e41f4b71Sopenharmony_ci| [FormLink](../reference/apis-arkui/arkui-ts/ts-container-formlink.md)               | No    | false         |
862e41f4b71Sopenharmony_ci| [GridCol](../reference/apis-arkui/arkui-ts/ts-container-gridcol.md) | Yes    | true         |
863e41f4b71Sopenharmony_ci| [GridRow](../reference/apis-arkui/arkui-ts/ts-container-gridrow.md) | Yes    | true         |
864e41f4b71Sopenharmony_ci| [Grid](../reference/apis-arkui/arkui-ts/ts-container-grid.md) | Yes    | true         |
865e41f4b71Sopenharmony_ci| [GridItem](../reference/apis-arkui/arkui-ts/ts-container-griditem.md) | Yes    | true         |
866e41f4b71Sopenharmony_ci| [Hyperlink](../reference/apis-arkui/arkui-ts/ts-container-hyperlink.md)         | Yes    | true         |
867e41f4b71Sopenharmony_ci| [List](../reference/apis-arkui/arkui-ts/ts-container-list.md) | Yes    | true         |
868e41f4b71Sopenharmony_ci| [ListItem](../reference/apis-arkui/arkui-ts/ts-container-listitem.md) | Yes    | true         |
869e41f4b71Sopenharmony_ci| [ListItemGroup](../reference/apis-arkui/arkui-ts/ts-container-listitemgroup.md) | Yes    | true         |
870e41f4b71Sopenharmony_ci| [Navigator](../reference/apis-arkui/arkui-ts/ts-container-navigator.md) | Yes    | true         |
871e41f4b71Sopenharmony_ci| [Refresh](../reference/apis-arkui/arkui-ts/ts-container-refresh.md) | Yes    | true        |
872e41f4b71Sopenharmony_ci| [RelativeContainer](../reference/apis-arkui/arkui-ts/ts-container-relativecontainer.md) | No    | false         |
873e41f4b71Sopenharmony_ci| [Row](../reference/apis-arkui/arkui-ts/ts-container-row.md) | Yes   | true         |
874e41f4b71Sopenharmony_ci| [RowSplit](../reference/apis-arkui/arkui-ts/ts-container-rowsplit.md) | Yes    | true         |
875e41f4b71Sopenharmony_ci| [Scroll](../reference/apis-arkui/arkui-ts/ts-container-scroll.md) | Yes    | true         |
876e41f4b71Sopenharmony_ci| [SideBarContainer](../reference/apis-arkui/arkui-ts/ts-container-sidebarcontainer.md) | Yes    | true         |
877e41f4b71Sopenharmony_ci| [Stack](../reference/apis-arkui/arkui-ts/ts-container-stack.md) | Yes    | true         |
878e41f4b71Sopenharmony_ci| [Swiper](../reference/apis-arkui/arkui-ts/ts-container-swiper.md) | Yes    | true         |
879e41f4b71Sopenharmony_ci| [Tabs](../reference/apis-arkui/arkui-ts/ts-container-tabs.md) | Yes    | true         |
880e41f4b71Sopenharmony_ci| [TabContent](../reference/apis-arkui/arkui-ts/ts-container-tabcontent.md) | Yes    | true         |
881e41f4b71Sopenharmony_ci| [WaterFlow](../reference/apis-arkui/arkui-ts/ts-container-waterflow.md)         | No    | false         |
882e41f4b71Sopenharmony_ci| [WithTheme](../reference/apis-arkui/arkui-ts/ts-container-with-theme.md)         | Yes    | true         |
883e41f4b71Sopenharmony_ci
884e41f4b71Sopenharmony_ci  **Table 3** Focusability of media components
885e41f4b71Sopenharmony_ci
886e41f4b71Sopenharmony_ci| Media Component                                    | Focusable| Default Value of focusable|
887e41f4b71Sopenharmony_ci| ---------------------------------------- | ----- | ------------ |
888e41f4b71Sopenharmony_ci| [Video](../reference/apis-arkui/arkui-ts/ts-media-components-video.md) | Yes    | true         |
889e41f4b71Sopenharmony_ci<!--no_check-->