• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Focus Control
2
3Focus control attributes set whether a component is focusable and how it participates in focus navigation.
4
5>  **NOTE**
6>
7>  - The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
8>
9>  - Custom components are inherently unfocusable, and setting [focusable](#focusable) and [enabled](ts-universal-attributes-enable.md#enabled) attributes to **false** or setting the [visibility](ts-universal-attributes-visibility.md#visibility) attribute to **Hidden** or **None** does not impact their child components' capability to gain focus.
10>
11>  - Components can actively acquire focus independently of the window's focus state.
12>
13>  - For details about focus development, see [Focus Event](../../../ui/arkts-common-events-focus-event.md).
14
15## focusable
16
17focusable(value: boolean)
18
19Sets whether the component is focusable.
20
21**Atomic service API**: This API can be used in atomic services since API version 11.
22
23**System capability**: SystemCapability.ArkUI.ArkUI.Full
24
25**Parameters**
26
27| Name| Type   | Mandatory| Description                                                        |
28| ------ | ------- | ---- | ------------------------------------------------------------ |
29| value  | boolean | Yes  | Whether the component is focusable.<br>**NOTE**<br>Components that have default interaction logic, such as [Button](ts-basic-components-button.md) and [TextInput](ts-basic-components-textinput.md), are focusable by default. Other components, such as [Text](ts-basic-components-text.md) and [Image](ts-basic-components-image.md), are not focusable by default. Only focusable components can trigger a [focus event](ts-universal-focus-event.md).|
30
31## tabIndex<sup>9+</sup>
32
33tabIndex(index: number)
34
35Sets the Tab order of the component in sequential focus navigation with the **Tab** key.
36
37**Atomic service API**: This API can be used in atomic services since API version 11.
38
39**System capability**: SystemCapability.ArkUI.ArkUI.Full
40
41**Parameters**
42
43| Name| Type  | Mandatory| Description                                                        |
44| ------ | ------ | ---- | ------------------------------------------------------------ |
45| index  | number | Yes  | Tab order of the component in sequential focus navigation with the **Tab** key. When components with positive **tabIndex** values are present, only these components are reachable through sequential focus navigation, and they are navigated cyclically in ascending order based on the **tabIndex** value. When components with positive **tabIndex** values are not present, those components with a **tabIndex** value of **0** are navigated based on the preset focus navigation rule.<br>**tabIndex** is not yet compatible with [UiExtension](../js-apis-arkui-uiExtension.md) component. As such, using **tabIndex** on a page that contains [UiExtension](../js-apis-arkui-uiExtension.md) may lead to disordered focus navigation.<br>- **tabIndex** >= 0: The component is focusable and can be reached through sequential keyboard navigation.<br>- **tabIndex** < 0 (usually **tabIndex** = -1): The component is focusable, but cannot be reached through sequential keyboard navigation.<br>Default value: **0**<br> **NOTE**<br> **tabIndex** and **focusScopeId** cannot be used together.|
46
47
48## defaultFocus<sup>9+</sup>
49
50defaultFocus(value: boolean)
51
52Specifies whether to set the component as the default focus of the page.
53
54**Atomic service API**: This API can be used in atomic services since API version 11.
55
56**System capability**: SystemCapability.ArkUI.ArkUI.Full
57
58**Parameters**
59
60| Name| Type   | Mandatory| Description                                                        |
61| ------ | ------- | ---- | ------------------------------------------------------------ |
62| value  | boolean | Yes  | Whether to set the component as the default focus of the page. This parameter takes effect only when the page is new and accessed for the first time.<br>Default value: **false**<br>**NOTE**<br>The value **true** means to set the component as the default focus, and the value **false** has no effect.<br>If no component on the page has **defaultFocus(true)** set:<br>For API version 11 and earlier, the default focus is on the first focusable non-container component on the page.<br>For API version versions later than 11, the default focus is on the page's root container.<br>If **defaultFocus(true)** is set for multiple components on the page, the first component found in the component tree in-depth traversal is used as the default focus.|
63
64## groupDefaultFocus<sup>9+</sup>
65
66groupDefaultFocus(value: boolean)
67
68Specifies whether to set the component as the default focus of the container.
69
70**Atomic service API**: This API can be used in atomic services since API version 11.
71
72**System capability**: SystemCapability.ArkUI.ArkUI.Full
73
74**Parameters**
75
76| Name| Type   | Mandatory| Description                                                        |
77| ------ | ------- | ---- | ------------------------------------------------------------ |
78| value  | boolean | Yes  | Whether to set the component as the default focus of the parent container. This parameter takes effect only when the container is new and obtains focus for the first time.<br>Default value: **false**<br>**NOTE**<br>This parameter must be used together with [tabIndex](#tabindex9). When **tabIndex** is set for a container and **groupDefaultFocus(true)** is set for a child in the container or for the container itself, then when the container obtains focus for the first time through sequential Tab navigation, the focus automatically moves to the specified component. If **groupDefaultFocus(true)** is set for multiple components in the container (including the container itself), the first component found in the component tree in-depth traversal receives the focus.|
79
80## focusOnTouch<sup>9+</sup>
81
82focusOnTouch(value: boolean)
83
84Sets whether the component is focusable on touch.
85
86**Atomic service API**: This API can be used in atomic services since API version 11.
87
88**System capability**: SystemCapability.ArkUI.ArkUI.Full
89
90**Parameters**
91
92| Name| Type   | Mandatory| Description                                                        |
93| ------ | ------- | ---- | ------------------------------------------------------------ |
94| value  | boolean | Yes  | Whether the component is focusable on touch.<br>Default value: **false**<br>**NOTE**<br>The component is focusable only when it is touchable.|
95
96## focusBox<sup>12+</sup>
97
98focusBox(style: FocusBoxStyle): T
99
100Sets the system focus box style for the component.
101
102**Atomic service API**: This API can be used in atomic services since API version 12.
103
104**System capability**: SystemCapability.ArkUI.ArkUI.Full
105
106**Parameters**
107
108| Name| Type| Mandatory| Description|
109| ---- | ---- | ---- | ---- |
110| style  | [FocusBoxStyle](#focusboxstyle12) | Yes  | System focus box style for the component.<br>**NOTE**<br>This style affects only the components that display the system focus box during focus traversal.|
111
112
113## focusControl<sup>9+</sup>
114
115Implements focus control.
116
117**Atomic service API**: This API can be used in atomic services since API version 11.
118
119### requestFocus<sup>9+</sup>
120
121requestFocus(value: string): boolean
122
123Requests the focus to move to the specified component. It is a global API. This API does not take effect in the current frame; the focus change will occur in the next frame. Use the [requestFocus](../js-apis-arkui-UIContext.md#requestfocus12) API in **FocusController** for immediate effect.
124
125**Atomic service API**: This API can be used in atomic services since API version 11.
126
127**Parameters**
128
129| Name| Type| Mandatory| Description|
130| ----- | ------ | ---- | ---- |
131| value | string | Yes  | String bound to the target component using **key(value: string)** or **id(value: string)**.|
132
133**Return value**
134
135| Type| Description|
136| ------- | ---- |
137| boolean | Returns whether the focus is successfully moved to the target component. Returns **true** if the specified component exists and the focus is successfully moved to the target component; returns **false** otherwise.|
138
139>  **NOTE**
140>
141>  The following components support focus control: [TextInput](ts-basic-components-textinput.md), [TextArea](ts-basic-components-textarea.md), [Search](ts-basic-components-search.md), [Button](ts-basic-components-button.md), [Text](ts-basic-components-text.md), [Image](ts-basic-components-image.md), [List](ts-container-list.md), [Grid](ts-container-grid.md). Currently, the running effect of the focus event can be displayed only on a real device.
142
143## FocusBoxStyle<sup>12+</sup>
144
145**Atomic service API**: This API can be used in atomic services since API version 12.
146
147| Name| Type| Mandatory| Description|
148| ---- | ---- | ---- | ---- |
149| margin  | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | No| Distance of the focus box from the component's edge.<br>A positive number indicates the outside, and a negative number indicates the inside. The value cannot be in percentage.|
150| strokeColor  | [ColorMetrics](../js-apis-arkui-graphics.md#colormetrics12) | No| Stroke color of the focus box.|
151| strokeWidth | [LengthMetrics](../js-apis-arkui-graphics.md#lengthmetrics12) | No| Stroke width of the focus box.<br>Negative numbers and percentages are not supported.|
152
153## focusScopePriority<sup>12+</sup>
154
155focusScopePriority(scopeId: string, priority?: FocusPriority): T
156
157Sets the focus priority of this component in a specified container. It must be used together with **focusScopeId**.
158
159**Atomic service API**: This API can be used in atomic services since API version 12.
160
161**System capability**: SystemCapability.ArkUI.ArkUI.Full
162
163**Parameters**
164
165| Name| Type   | Mandatory| Description                                                        |
166| ------ | ------- | ---- | ------------------------------------------------------------ |
167| scopeId  | string | Yes  | ID of the container component where the current component's focus priority takes effect.<br>**NOTE**<br>1. The current component must be within the container identified by **scopeId**, or the container to which the current component belongs must be within the container identified by **scopeId**.<br>2. A component cannot set multiple priorities.<br>3. A container component with **focusScopeId** set cannot have its priority set.|
168| priority  | [FocusPriority](#focuspriority12)  | No  | Focus priority.<br>**NOTE**<br>If **priority** is not set, the component uses the default **AUTO** priority.<br>Impact of the priority on focus traversal and component focus:<br>1. When the container gains focus as a whole (page level switching/focus switching to a focus group/container component requesting focus with **requestFocus**), if there is a component with a priority of **PREVIOUS** within the container, that component gains focus; otherwise, the last focused component does.<br>2. When a container does not gain focus as a whole (using **Tab** or arrow keys in non-focus group scenarios), the highest priority component gets focus on first focus; subsequent focus follows position order regardless of priority.|
169
170### FocusPriority<sup>12+</sup>
171
172**Atomic service API**: This API can be used in atomic services since API version 12.
173
174**System capability**: SystemCapability.ArkUI.ArkUI.Full
175
176| Name         | Description       |
177| ----------- | --------- |
178| AUTO | Default priority, that is, the focus priority assigned by default.|
179| PRIOR | Priority that indicates the component is prioritized in the container. This level is higher than **AUTO**.|
180| PREVIOUS | Priority of a previously focused node in the container. This level is higher than **PRIOR**.|
181
182### KeyProcessingMode<sup>15+</sup>
183
184Enumerates the modes for processing key events.
185
186**Atomic service API**: This API can be used in atomic services since API version 15.
187
188**System capability**: SystemCapability.ArkUI.ArkUI.Full
189
190| Name    | Value   | Description       |
191| -----------| ----------- | --------- |
192| FOCUS_NAVIGATION  | 0 | Default value. When the current component does not consume the key event, focus navigation using the **Tab** and arrow keys preferentially stays within the current container.|
193| ANCESTOR_EVENT  | 1  |  When the current component does not consume the key event, focus navigation using the **Tab** and arrow keys is bubbled up to the parent component.|
194
195## focusScopeId<sup>12+</sup>
196
197focusScopeId(id: string, isGroup?: boolean)
198
199Assigns an ID to this container component and specifies whether the container is a focus group.
200
201**Atomic service API**: This API can be used in atomic services since API version 12.
202
203**System capability**: SystemCapability.ArkUI.ArkUI.Full
204
205**Parameters**
206
207| Name| Type   | Mandatory| Description                                                        |
208| ------ | ------- | ---- | ------------------------------------------------------------ |
209| id  | string | Yes  | ID of the current container component.<br>**NOTE**<br>The ID must be unique within a single level page.|
210| isGroup  | boolean | No  | Whether the current container component is a focus group.<br>**NOTE**<br>Focus groups cannot be nested and should not be configured repeatedly.<br> The focus group and **tabIndex** cannot be used together.<br>The focus group enables the container and its elements to navigate focus according to the focus group rules as follows:<br>1. Only arrow keys are allowed for focus traversal within the focus group; the **Tab** key will move the focus out of the focus group.<br>2. When arrow keys are used to move the focus from outside the focus group to inside, if there is a component with a priority of **PREVIOUS** within the focus group, that component gains focus; otherwise, the last focused component does.|
211
212## focusScopeId<sup>14+</sup>
213
214focusScopeId(id: string, isGroup?: boolean, arrowStepOut?: boolean)
215
216Assigns an ID to this container component and specifies whether the container is a focus group.
217
218**System capability**: SystemCapability.ArkUI.ArkUI.Full
219
220**Parameters**
221
222| Name| Type   | Mandatory| Description                                                        |
223| ------ | ------- | ---- | ------------------------------------------------------------ |
224| id  | string | Yes  | ID of the current container component.<br>**NOTE**<br>The ID must be unique within a single level page.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
225| isGroup  | boolean | No  | Whether the current container component is a focus group.<br>**NOTE**<br>Focus groups cannot be nested and should not be configured repeatedly.<br> The focus group and **tabIndex** cannot be used together.<br>The focus group enables the container and its elements to navigate focus according to the focus group rules as follows:<br>1. Only arrow keys are allowed for focus traversal within the focus group; the **Tab** key will move the focus out of the focus group.<br>2. When arrow keys are used to move the focus from outside the focus group to inside, if there is a component with a priority of **PREVIOUS** within the focus group, that component gains focus; otherwise, the last focused component does.<br>**Atomic service API**: This API can be used in atomic services since API version 12.|
226| arrowStepOut<sup>14+</sup>  | boolean | No  | Whether the focus can be moved out of the current focus group using arrow keys.<br>**Atomic service API**: This API can be used in atomic services since API version 14.|
227
228## tabStop<sup>14+</sup>
229
230tabStop(isTabStop: boolean) :T
231
232Sets whether this container component is a focus stop. During focus traversal, the focus stops at the container component serving as a focus stop.
233
234**Atomic service API**: This API can be used in atomic services since API version 14.
235
236**System capability**: SystemCapability.ArkUI.ArkUI.Full
237
238**Parameters**
239
240| Name| Type   | Mandatory| Description                                                        |
241| ------ | ------- | ---- | ------------------------------------------------------------ |
242| isTabStop  | boolean | Yes  | Whether the current container component is a focus stop.<br>**NOTE**<br>1. To configure **tabStop**, make sure the component is a container and has focusable child components. By default, container components cannot directly gain focus.<br> 2. When [requestFocus](../js-apis-arkui-UIContext.md#requestfocus12) is used for requesting focus, if the component is a container and **tabStop** is configured, the focus can stop at the container. If **tabStop** is not configured, the component can still gain focus, even if there are other components in the focus chain with **tabStop** configured.<br>3. Containers with **tabStop** configured should not be nested more than two levels deep.<br>Focus traversal rules with **tabStop**:<br>1. During focus traversal using the **Tab** key or arrow keys, the focus stops at components with **tabStop** configured. If the focus is inside a container with **tabStop** configured, it can move to the next focusable component within the container. If the focus is outside such a container, it can move to the next focusable component outside the container.<br>2. When the focus is on a container with **tabStop** configured, pressing **Enter** moves the focus to the first focusable component inside the container. Pressing **ESC** moves the focus back to the previous component with **tabStop** configured, without exceeding the current level of the page root container. Pressing the spacebar triggers the **onClick** event of the container.<br>3. Whenever possible, avoid configuring **tabStop** on the root container. If **tabStop** is configured on the root container, the following behaviors will occur:<br>- Using [clearFocus](../js-apis-arkui-UIContext.md#clearfocus12) to clear the focus to the root container and then pressing **Enter** will move the focus to the previously focused component inside the root container.<br>- Using **ESC** to clear the focus to the root container and then pressing **Enter** will move the focus to the first focusable component inside the root container.|
243
244**Example for describing the keys and focusable components during focus traversal**
245
246![tabStop](figures/tabStop.png)
247
248If the current focus is on **button2**, pressing the **Tab** key will move the focus to **Column3**. Pressing the **Tab** key again will move the focus back to **button1**.
249
250## Example
251
252### Example 1: Setting Focus and Focus Traversal Effects for Components
253
254This example shows how to use **defaultFocus**, **groupDefaultFocus**, and **focusOnTouch**. **defaultFocus** sets the bound component as the initial focus after the page is created. **groupDefaultFocus** sets the bound component as the initial focus after the container with the specified **tabIndex** is created. **focusOnTouch** sets the bound component to obtain focus upon being clicked.
255
256```ts
257// focusTest.ets
258@Entry
259@Component
260struct FocusableExample {
261  @State inputValue: string = ''
262
263  build() {
264    Scroll() {
265      Row({ space: 20 }) {
266        Column({ space: 20 }) {
267          Column({ space: 5 }) {
268            Button('Group1')
269              .width(165)
270              .height(40)
271              .fontColor(Color.White)
272              .focusOnTouch(true)           // The button is focusable on touch.
273            Row({ space: 5 }) {
274              Button()
275                .width(80)
276                .height(40)
277                .fontColor(Color.White)
278              Button()
279                .width(80)
280                .height(40)
281                .fontColor(Color.White)
282                .focusOnTouch(true)           // The button is focusable on touch.
283            }
284            Row({ space: 5 }) {
285              Button()
286                .width(80)
287                .height(40)
288                .fontColor(Color.White)
289              Button()
290                .width(80)
291                .height(40)
292                .fontColor(Color.White)
293            }
294          }.borderWidth(2).borderColor(Color.Red).borderStyle(BorderStyle.Dashed)
295          .tabIndex(1)                      // The column is the initial component to have focus in sequential keyboard navigation.
296          Column({ space: 5 }) {
297            Button('Group2')
298              .width(165)
299              .height(40)
300              .fontColor(Color.White)
301            Row({ space: 5 }) {
302              Button()
303                .width(80)
304                .height(40)
305                .fontColor(Color.White)
306              Button()
307                .width(80)
308                .height(40)
309                .fontColor(Color.White)
310                .groupDefaultFocus(true)      // The button obtains focus when its upper-level column is in focus.
311            }
312            Row({ space: 5 }) {
313              Button()
314                .width(80)
315                .height(40)
316                .fontColor(Color.White)
317              Button()
318                .width(80)
319                .height(40)
320                .fontColor(Color.White)
321            }
322          }.borderWidth(2).borderColor(Color.Green).borderStyle(BorderStyle.Dashed)
323          .tabIndex(2)                      // The column is the second component to have focus in sequential keyboard navigation.
324        }
325        Column({ space: 5 }) {
326          TextInput({placeholder: 'input', text: this.inputValue})
327            .onChange((value: string) => {
328              this.inputValue = value
329            })
330            .width(156)
331            .defaultFocus(true)             // The <TextInput> component is the initial default focus of the page.
332          Button('Group3')
333            .width(165)
334            .height(40)
335            .fontColor(Color.White)
336          Row({ space: 5 }) {
337            Button()
338              .width(80)
339              .height(40)
340              .fontColor(Color.White)
341            Button()
342              .width(80)
343              .height(40)
344              .fontColor(Color.White)
345          }
346          Button()
347            .width(165)
348            .height(40)
349            .fontColor(Color.White)
350          Row({ space: 5 }) {
351            Button()
352              .width(80)
353              .height(40)
354              .fontColor(Color.White)
355            Button()
356              .width(80)
357              .height(40)
358              .fontColor(Color.White)
359          }
360          Button()
361            .width(165)
362            .height(40)
363            .fontColor(Color.White)
364          Row({ space: 5 }) {
365            Button()
366              .width(80)
367              .height(40)
368              .fontColor(Color.White)
369            Button()
370              .width(80)
371              .height(40)
372              .fontColor(Color.White)
373          }
374        }.borderWidth(2).borderColor(Color.Orange).borderStyle(BorderStyle.Dashed)
375        .tabIndex(3)                      // The column is the third component to have focus in sequential keyboard navigation.
376      }.alignItems(VerticalAlign.Top)
377    }
378  }
379}
380```
381Diagrams:
382
383On first-time access, the focus is on the **TextInput** component bound to **defaultFocus**.
384
385![defaultFocus](figures/defaultFocus.png)
386
387When you press the **Tab** key for the first time, the focus switches to the container that matches **tabIndex(1)** and automatically moves to the component bound to **groupDefaultFocus**.
388
389![groupDefaultFocus1](figures/groupDefaultFocus1.png)
390
391When you press the **Tab** key for the second time, the focus switches to the container that matches **tabIndex(2)** and automatically moves to the component bound to **groupDefaultFocus**.
392
393![groupDefaultFocus2](figures/groupDefaultFocus2.png)
394
395When you press the **Tab** key for the third time, the focus switches to the container that matches **tabIndex(3)** and automatically moves to the component bound to **groupDefaultFocus**.
396
397![groupDefaultFocus3](figures/groupDefaultFocus3.png)
398
399Clicking the component bound to **focusOnTouch** sets the focus on the component and removes the focus indicator. Pressing the Tab key again displays the focus indicator.
400
401![focusOnTouch](figures/focusOnTouch.png)
402
403### Example 2: Setting Focus on a Specific Component
404
405This example demonstrates how to set focus on a specific component using **focusControl.requestFocus**.
406
407> **NOTE**
408>
409> To avoid confusion with **focusControl** instances, it is recommended that you obtain a **UIContext** instance using the [getUIContext](../js-apis-arkui-UIContext.md#uicontext) API, and then obtain the **focusControl** instance bound to the context through the [getFocusController](../js-apis-arkui-UIContext.md#getfocuscontroller12) API.
410
411```ts
412// requestFocus.ets
413@Entry
414@Component
415struct RequestFocusExample {
416  @State idList: string[] = ['A', 'B', 'C', 'D', 'E', 'F', 'LastPageId']
417  @State selectId: string = 'LastPageId'
418
419  build() {
420    Column({ space:20 }){
421      Row({space: 5}) {
422        Button("id: " + this.idList[0] + " focusable(false)")
423          .width(200).height(70).fontColor(Color.White)
424          .id(this.idList[0])
425          .focusable(false)
426        Button("id: " + this.idList[1])
427          .width(200).height(70).fontColor(Color.White)
428          .id(this.idList[1])
429      }
430      Row({space: 5}) {
431        Button("id: " + this.idList[2])
432          .width(200).height(70).fontColor(Color.White)
433          .id(this.idList[2])
434        Button("id: " + this.idList[3])
435          .width(200).height(70).fontColor(Color.White)
436          .id(this.idList[3])
437      }
438      Row({space: 5}) {
439        Button("id: " + this.idList[4])
440          .width(200).height(70).fontColor(Color.White)
441          .id(this.idList[4])
442        Button("id: " + this.idList[5])
443          .width(200).height(70).fontColor(Color.White)
444          .id(this.idList[5])
445      }
446      Row({space: 5}) {
447        Select([{value: this.idList[0]},
448                {value: this.idList[1]},
449                {value: this.idList[2]},
450                {value: this.idList[3]},
451                {value: this.idList[4]},
452                {value: this.idList[5]},
453                {value: this.idList[6]}])
454          .value(this.selectId)
455          .onSelect((index: number) => {
456            this.selectId = this.idList[index]
457          })
458        Button("RequestFocus")
459          .width(200).height(70).fontColor(Color.White)
460          .onClick(() => {
461            // You are advised to use this.getUIContext().getFocusController().requestFocus().
462            let res = focusControl.requestFocus(this.selectId)      // Move the focus to the component specified by this.selectId.
463            if (res) {
464              this.getUIContext().getPromptAction().showToast({message: 'Request success'})
465            } else {
466              this.getUIContext().getPromptAction().showToast({message: 'Request failed'})
467            }
468          })
469      }
470    }.width('100%').margin({ top:20 })
471  }
472}
473```
474
475Diagrams:
476
477Press the **Tab** key to activate the focus state.
478Below shows how the UI behaves when you request focus for a component that does not exist.
479
480![requestFocus1](figures/requestFocus1.png)
481
482Below shows how the UI behaves when you request focus for a component that is not focusable.
483
484![requestFocus2](figures/requestFocus2.png)
485
486Below shows how the UI behaves when you request focus for a focusable component.
487
488![requestFocus3](figures/requestFocus3.png)
489
490### Example 3: Customizing the Focus Box Style
491
492This example shows how to change the focus box style of a component by configuring **focusBox**.
493
494```ts
495import { ColorMetrics, LengthMetrics } from '@kit.ArkUI'
496
497@Entry
498@Component
499struct RequestFocusExample {
500  build() {
501    Column({ space: 30 }) {
502      Button("small black focus box")
503        .focusBox({
504          margin: new LengthMetrics(0),
505          strokeColor: ColorMetrics.rgba(0, 0, 0),
506        })
507      Button("large red focus box")
508        .focusBox({
509          margin: LengthMetrics.px(20),
510          strokeColor: ColorMetrics.rgba(255, 0, 0),
511          strokeWidth: LengthMetrics.px(10)
512        })
513    }
514    .alignItems(HorizontalAlign.Center)
515    .width('100%')
516  }
517}
518```
519
520![focusBox](figures/focusBox.gif)
521
522
523### Example 4: Setting Focus Group Traversal
524
525This example demonstrates how to set a component as the initial focus when its container gains focus by configuring **focusScopePriority**. Configuring **focusScopeId** allows the bound container component to become a focus group.
526
527```ts
528// focusTest.ets
529@Entry
530@Component
531struct FocusableExample {
532  @State inputValue: string = ''
533
534  build() {
535    Scroll() {
536      Row({ space: 20 }) {
537        Column({ space: 20 }) {  // Labeled as Column1.
538          Column({ space: 5 }) {
539            Button('Group1')
540              .width(165)
541              .height(40)
542              .fontColor(Color.White)
543            Row({ space: 5 }) {
544              Button()
545                .width(80)
546                .height(40)
547                .fontColor(Color.White)
548              Button()
549                .width(80)
550                .height(40)
551                .fontColor(Color.White)
552            }
553            Row({ space: 5 }) {
554              Button()
555                .width(80)
556                .height(40)
557                .fontColor(Color.White)
558              Button()
559                .width(80)
560                .height(40)
561                .fontColor(Color.White)
562            }
563          }.borderWidth(2).borderColor(Color.Red).borderStyle(BorderStyle.Dashed)
564          Column({ space: 5 }) {
565            Button('Group2')
566              .width(165)
567              .height(40)
568              .fontColor(Color.White)
569            Row({ space: 5 }) {
570              Button()
571                .width(80)
572                .height(40)
573                .fontColor(Color.White)
574              Button()
575                .width(80)
576                .height(40)
577                .fontColor(Color.White)
578                .focusScopePriority('ColumnScope1', FocusPriority.PRIOR) // Focus when Column1 first gains focus.
579            }
580            Row({ space: 5 }) {
581              Button()
582                .width(80)
583                .height(40)
584                .fontColor(Color.White)
585              Button()
586                .width(80)
587                .height(40)
588                .fontColor(Color.White)
589            }
590          }.borderWidth(2).borderColor(Color.Green).borderStyle(BorderStyle.Dashed)
591        }
592        .focusScopeId('ColumnScope1')
593        Column({ space: 5 }) {  // Labeled as Column2.
594          TextInput({placeholder: 'input', text: this.inputValue})
595            .onChange((value: string) => {
596              this.inputValue = value
597            })
598            .width(156)
599          Button('Group3')
600            .width(165)
601            .height(40)
602            .fontColor(Color.White)
603          Row({ space: 5 }) {
604            Button()
605              .width(80)
606              .height(40)
607              .fontColor(Color.White)
608            Button()
609              .width(80)
610              .height(40)
611              .fontColor(Color.White)
612          }
613          Button()
614            .width(165)
615            .height(40)
616            .fontColor(Color.White)
617            .focusScopePriority('ColumnScope2', FocusPriority.PREVIOUS)  // Focuses when Column2 first gains focus.
618          Row({ space: 5 }) {
619            Button()
620              .width(80)
621              .height(40)
622              .fontColor(Color.White)
623            Button()
624              .width(80)
625              .height(40)
626              .fontColor(Color.White)
627          }
628          Button()
629            .width(165)
630            .height(40)
631            .fontColor(Color.White)
632          Row({ space: 5 }) {
633            Button()
634              .width(80)
635              .height(40)
636              .fontColor(Color.White)
637            Button()
638              .width(80)
639              .height(40)
640              .fontColor(Color.White)
641          }
642        }.borderWidth(2).borderColor(Color.Orange).borderStyle(BorderStyle.Dashed)
643        .focusScopeId('ColumnScope2', true) // Column2 is a focus group.
644      }.alignItems(VerticalAlign.Top)
645    }
646  }
647}
648```
649
650### Example 5: Setting Focus Stop
651
652This example illustrates how to use **tabStop** to make the focus stop on a component during focus traversal with the **Tab** key.
653
654```ts
655import { ColorMetrics, LengthMetrics } from '@kit.ArkUI'
656
657@Entry
658@Component
659struct TabStop {
660  build() {
661    Column({ space: 20 }) {
662      Column({ space: 20 }) {
663        Column({ space: 20 }) {
664          Row({ space: 5 }) {
665            Button("button 1")
666              .width(200).height(70).fontColor(Color.White)
667              .focusBox({
668                margin: LengthMetrics.px(20),
669                strokeColor: ColorMetrics.rgba(23, 169, 141),
670                strokeWidth: LengthMetrics.px(10)
671              })
672          }
673          Row({ space: 5 }) {
674            Button("button 2")
675              .width(200).height(70).fontColor(Color.White)
676              .focusBox({
677                margin: LengthMetrics.px(20),
678                strokeColor: ColorMetrics.rgba(23, 169, 141),
679                strokeWidth: LengthMetrics.px(10)
680              })
681          }
682        }.width('80%').margin({ top: 30 }).borderColor(Color.Black)
683      }.width('95%').margin({ top: 60 }).borderColor(Color.Black)
684      Column({ space: 20 }) {
685        Column({ space: 20 }) {
686          Row({ space: 5 }) {
687            Button("button 3")
688              .width(200)
689              .height('70%')
690              .fontColor(Color.White)
691              .focusBox({
692                margin: LengthMetrics.px(20),
693                strokeColor: ColorMetrics.rgba(23, 169, 141),
694                strokeWidth: LengthMetrics.px(10)
695              })
696              .margin({ top: 15 })
697          }
698        }
699        .width('80%')
700        .height('120')
701        .borderColor(Color.Black)
702        .margin({ top: 10 })
703        .tabStop(true)
704        .focusBox({
705          margin: LengthMetrics.px(20),
706          strokeColor: ColorMetrics.rgba(23, 169, 141),
707          strokeWidth: LengthMetrics.px(10)
708        })
709        .borderWidth(1)
710      }.width('95%').margin({ top: 50 }).borderColor(Color.Black)
711    }
712  }
713}
714```
715Diagrams:
716
717Pressing **Tab** twice moves the focus to **button2**.
718
719![tabStop2](figures/tabStop2.png)
720
721Pressing **Tab** again moves the focus to the component configured with **tabStop**.
722
723![tabStop3](figures/tabStop3.png)
724
725Pressing **Enter** moves the focus to **button3**.
726
727![tabStop4](figures/tabStop4.png)
728
729Pressing **ESC** again moves the focus to the component configured with **tabStop**.
730
731![tabStop3](figures/tabStop3.png)
732
733Pressing **Tab** moves the focus back to **button1**.
734
735![tabStop1](figures/tabStop1.png)
736