ts-universal-attributes-focus.md 3.0 KB
Newer Older
Z
zengyawen 已提交
1 2
# Focus Control

3 4
>  **NOTE**<br>
> The APIs of this module are supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
5 6 7 8 9 10 11 12 13


## Required Permissions

None


## Attributes

14
| Name| Type| Default Value| Description|
Z
zengyawen 已提交
15
| -------- | -------- | -------- | -------- |
16 17
| focusable | boolean | false | Whether the current component is focusable.|
| tabIndex<sup>9+<sup> | number | 0 | How the current component participates in sequential keyboard navigation.<br>- **tabIndex** >= 0: The component is focusable in sequential keyboard navigation, with its order defined by the value. A component with a larger value is focused after one with a smaller value. If multiple components share the same **tabIndex** value, their focus order follows their position in the component tree.<br>- **tabIndex** < 0 (usually **tabIndex** = -1): The component is focusable, but cannot be reached through sequential keyboard navigation. |
Z
zengyawen 已提交
18

19 20
>  **NOTE**<br>
> The following components support focus control: **\<Button>**, **\<Text>**, **\<Image>**, **\<List>**, and **\<Grid>**.
Z
zengyawen 已提交
21 22 23 24


## Example

25 26
```ts
// xxx.ets
Z
zengyawen 已提交
27 28 29
@Entry
@Component
struct FocusableExample {
30 31 32 33 34 35 36 37
  @State text1: string = 'TabIndex=3'
  @State text2: string = 'TabIndex=2'
  @State text3: string = 'focusable=false'
  @State text4: string = 'TabIndex=-1'
  @State text5: string = 'TabIndex=1'
  @State text6: string = 'TabIndex=1'
  @State text7: string = 'focusable=true'
  @State text8: string = 'focusable=true'
Z
zengyawen 已提交
38 39 40

  build() {
    Column({ space:20 }){
41
      Button(this.text1)  // This component is the fourth component that is focused when the Tab key is pressed.
Z
zengyawen 已提交
42
        .width(300).height(70).fontColor(Color.Black)
43 44
        .tabIndex(3)
      Button(this.text2)  // This component is the third component that is focused when the Tab key is pressed.
Z
zengyawen 已提交
45
        .width(300).height(70).fontColor(Color.Black)
46 47
        .tabIndex(2)
      Button(this.text3)  // This component is not focusable.
Z
zengyawen 已提交
48 49
        .width(300).height(70).fontColor(Color.Black)
        .focusable(false)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
      Button(this.text4) // This component is focusable, but cannot be reached through sequential keyboard navigation.
        .width(300).height(70).fontColor(Color.Black)
        .tabIndex(-1)
      Button(this.text5)  // This component is the first component that is focused when the Tab key is pressed.
        .width(300).height(70).fontColor(Color.Black)
        .tabIndex(1)
      Button(this.text6)  // This component is the second component that is focused when the Tab key is pressed.
        .width(300).height(70).fontColor(Color.Black)
        .tabIndex(1)
      Button(this.text7)  // This component is the fifth component that is focused when the Tab key is pressed.
        .width(300).height(70).fontColor(Color.Black)
        .focusable(true)
      Button(this.text8)  // This component is the sixth component that is focused when the Tab key is pressed.
        .width(300).height(70).fontColor(Color.Black)
        .focusable(true)
Z
zengyawen 已提交
65 66 67 68
    }.width('100%').margin({ top:20 })
  }
}
```