ts-universal-attributes-focus.md 2.8 KB
Newer Older
Z
zengyawen 已提交
1 2
# 焦点控制

T
explain  
tianyu 已提交
3 4
自定义组件的走焦效果,可设置组件是否走焦和具体的走焦顺序,tab键或者方向键切换焦点。

H
geshi  
HelloCrease 已提交
5
>  **说明:**
Z
zengyawen 已提交
6 7 8 9 10 11 12 13 14 15
> 从API Version 8开始支持。后续版本如有新增内容,则采用上角标单独标记该内容的起始版本。


## 权限列表




## 属性

T
explain  
tianyu 已提交
16
| **名称** | **参数类型** | **默认值** | **描述** |
Z
zengyawen 已提交
17
| -------- | -------- | -------- | -------- |
T
explain  
tianyu 已提交
18
| focusable | boolean | false | 设置当前组件是否可以获焦。 |
B
bixuefeng 已提交
19
| tabIndex<sup>9+<sup> | number | 0 | 自定义组件tab键走焦能力,走焦顺序为:tabIndex大于0的组件依次递增走焦, tabIndex等于0的组件按组件树先后顺序走焦。<br />- tabIndex >= 0:表示元素是可聚焦的,并且可以通过tab键走焦来访问到该元素,按照tabIndex的数值递增而先后获焦。如果多个元素拥有相同的tabIndex,按照元素在当前组件树中的先后顺序获焦<br />- tabIndex < 0(通常是tabIndex = -1):表示元素是可聚焦的,但是不能通过tab键走焦来访问到该元素 |
Z
zengyawen 已提交
20

H
geshi  
HelloCrease 已提交
21
>  **说明:**
Z
zengyawen 已提交
22 23 24 25
> 支持焦点控制的组件:Button、Text、Image、List、Grid。

## 示例

T
tianyu 已提交
26 27
焦点事件当前仅支持在真机上显示运行效果。

H
geshi  
HelloCrease 已提交
28 29
```ts
// xxx.ets
Z
zengyawen 已提交
30 31 32
@Entry
@Component
struct FocusableExample {
B
bixuefeng 已提交
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 }){
B
bixuefeng 已提交
44
      Button(this.text1)  // 按下TAB键第四个获焦的组件
Z
zengyawen 已提交
45
        .width(300).height(70).fontColor(Color.Black)
B
bixuefeng 已提交
46 47
        .tabIndex(3)
      Button(this.text2)  // 按下TAB键第三个获焦的组件
Z
zengyawen 已提交
48
        .width(300).height(70).fontColor(Color.Black)
B
bixuefeng 已提交
49 50
        .tabIndex(2)
      Button(this.text3)  // 无法获焦的组件
Z
zengyawen 已提交
51 52
        .width(300).height(70).fontColor(Color.Black)
        .focusable(false)
B
bixuefeng 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
      Button(this.text4)  // 无法使用TAB键,但可以使用方向键走焦的组件
        .width(300).height(70).fontColor(Color.Black)
        .tabIndex(-1)
      Button(this.text5)  // 按下TAB键第一个获焦的组件
        .width(300).height(70).fontColor(Color.Black)
        .tabIndex(1)
      Button(this.text6)  // 按下TAB键第二个获焦的组件
        .width(300).height(70).fontColor(Color.Black)
        .tabIndex(1)
      Button(this.text7)  // 按下TAB键第五个获焦的组件
        .width(300).height(70).fontColor(Color.Black)
        .focusable(true)
      Button(this.text8)  // 按下TAB键第六个获焦的组件
        .width(300).height(70).fontColor(Color.Black)
        .focusable(true)
Z
zengyawen 已提交
68 69 70 71
    }.width('100%').margin({ top:20 })
  }
}
```
T
tianyu 已提交
72 73

![focus](figures/focus.png)