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

E
ester.zhou 已提交
3 4 5
>  **NOTE**
>
>  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 已提交
6 7 8 9 10 11 12 13 14


## Required Permissions

None


## Attributes

15
| Name| Type| Default Value| Description|
Z
zengyawen 已提交
16
| -------- | -------- | -------- | -------- |
17 18
| 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 已提交
19

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

## Example

E
ester.zhou 已提交
26 27
The effects of focus events can only be checked on real devices.

28 29
```ts
// xxx.ets
Z
zengyawen 已提交
30 31 32
@Entry
@Component
struct FocusableExample {
33 34 35 36 37 38 39 40
  @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 已提交
41 42 43

  build() {
    Column({ space:20 }){
44
      Button(this.text1)  // This component is the fourth component that is focused when the Tab key is pressed.
Z
zengyawen 已提交
45
        .width(300).height(70).fontColor(Color.Black)
46 47
        .tabIndex(3)
      Button(this.text2)  // This component is the third component that is focused when the Tab key is pressed.
Z
zengyawen 已提交
48
        .width(300).height(70).fontColor(Color.Black)
49 50
        .tabIndex(2)
      Button(this.text3)  // This component is not focusable.
Z
zengyawen 已提交
51 52
        .width(300).height(70).fontColor(Color.Black)
        .focusable(false)
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
      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 已提交
68 69 70 71
    }.width('100%').margin({ top:20 })
  }
}
```
E
ester.zhou 已提交
72 73

![focus](figures/focus.png)