焦點控制

2024-01-22 16:19 更新

自定義組件的走焦效果,可設(shè)置組件是否走焦和具體的走焦順序,tab鍵或者方向鍵切換焦點。

說明

從API Version 8開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨標(biāo)記該內(nèi)容的起始版本。

屬性

名稱

參數(shù)類型

描述

focusable

boolean

設(shè)置當(dāng)前組件是否可以獲焦。

說明:

存在默認交互邏輯的組件例如Button、TextInput等,默認即為可獲焦,Text、Image等組件則默認狀態(tài)為不可獲焦。不可獲焦?fàn)顟B(tài)下,無法觸發(fā)焦點事件。

tabIndex9+

number

自定義組件tab鍵走焦能力。若有配置了tabIndex大于0的組件,則tab鍵走焦只會在tabIndex大于0的組件內(nèi)按照tabIndex的值從小到大并循環(huán)依次走焦。若沒有配置tabIndex大于0的組件,則tabIndex等于0的組件按照組件預(yù)設(shè)的走焦規(guī)則走焦。

- tabIndex >= 0:表示元素是可聚焦的,并且可以通過tab鍵走焦來訪問到該元素。

- tabIndex < 0(通常是tabIndex = -1):表示元素是可聚焦的,但是不能通過tab鍵走焦來訪問到該元素。

默認值:0

defaultFocus9+

boolean

設(shè)置當(dāng)前組件是否為當(dāng)前頁面上的默認焦點,僅在初次創(chuàng)建的頁面第一次進入時生效。

默認值:false

groupDefaultFocus9+

boolean

設(shè)置當(dāng)前組件是否為當(dāng)前組件所在容器獲焦時的默認焦點,僅在初次創(chuàng)建容器節(jié)點第一次獲焦時生效。

默認值:false

說明: 必須與tabIndex聯(lián)合使用,當(dāng)某個容器設(shè)置了tabIndex,且容器內(nèi)某子組件設(shè)置了groupDefaultFocus,當(dāng)該容器首次獲焦時,會自動將焦點轉(zhuǎn)移至該組件上。

focusOnTouch9+

boolean

設(shè)置當(dāng)前組件是否支持點擊獲焦能力。

默認值:false

說明: 僅在組件可點擊時才能正常獲取焦點。

focusControl9+

requestFocus9+

requestFocus(value: string): boolean

方法語句中可使用的全局接口,調(diào)用此接口可以主動讓焦點轉(zhuǎn)移至參數(shù)指定的組件上。

參數(shù):

名稱

類型

必填

描述

value

string

目標(biāo)組件使用接口key(value: string)綁定的字符串。

返回值:

類型

說明

boolean

返回是否成功給目標(biāo)組件申請到焦點。若參數(shù)指向的目標(biāo)組件存在,且目標(biāo)組件可獲焦,則返回true,否則返回false。

說明

支持焦點控制的組件:TextInput、TextArea、Search、Button、Text、Image、List、Grid。焦點事件當(dāng)前僅支持在真機上顯示運行效果。

示例

示例1

defaultFocus/groupDefaultFocus/focusOnTouch示例代碼:

defaultFocus可以使綁定的組件成為頁面創(chuàng)建后首次獲焦的焦點。groupDefaultFocus可以使綁定的組件成為tabIndex容器創(chuàng)建后首次獲焦的焦點。focusOnTouch可以使綁定的組件點擊后立即獲焦。

  1. // focusTest.ets
  2. @Entry
  3. @Component
  4. struct FocusableExample {
  5. @State inputValue: string = ''
  6. build() {
  7. Scroll() {
  8. Row({ space: 20 }) {
  9. Column({ space: 20 }) {
  10. Column({ space: 5 }) {
  11. Button('Group1')
  12. .width(165)
  13. .height(40)
  14. .fontColor(Color.White)
  15. .focusOnTouch(true) // 該Button組件點擊后可獲焦
  16. Row({ space: 5 }) {
  17. Button()
  18. .width(80)
  19. .height(40)
  20. .fontColor(Color.White)
  21. Button()
  22. .width(80)
  23. .height(40)
  24. .fontColor(Color.White)
  25. .focusOnTouch(true) // 該Button組件點擊后可獲焦
  26. }
  27. Row({ space: 5 }) {
  28. Button()
  29. .width(80)
  30. .height(40)
  31. .fontColor(Color.White)
  32. Button()
  33. .width(80)
  34. .height(40)
  35. .fontColor(Color.White)
  36. }
  37. }.borderWidth(2).borderColor(Color.Red).borderStyle(BorderStyle.Dashed)
  38. .tabIndex(1) // 該Column組件為按TAB鍵走焦的第一個獲焦的組件
  39. Column({ space: 5 }) {
  40. Button('Group2')
  41. .width(165)
  42. .height(40)
  43. .fontColor(Color.White)
  44. Row({ space: 5 }) {
  45. Button()
  46. .width(80)
  47. .height(40)
  48. .fontColor(Color.White)
  49. Button()
  50. .width(80)
  51. .height(40)
  52. .fontColor(Color.White)
  53. .groupDefaultFocus(true) // 該Button組件上級Column組件獲焦時獲焦
  54. }
  55. Row({ space: 5 }) {
  56. Button()
  57. .width(80)
  58. .height(40)
  59. .fontColor(Color.White)
  60. Button()
  61. .width(80)
  62. .height(40)
  63. .fontColor(Color.White)
  64. }
  65. }.borderWidth(2).borderColor(Color.Green).borderStyle(BorderStyle.Dashed)
  66. .tabIndex(2) // 該Column組件為按TAB鍵走焦的第二個獲焦的組件
  67. }
  68. Column({ space: 5 }) {
  69. TextInput({placeholder: 'input', text: this.inputValue})
  70. .onChange((value: string) => {
  71. this.inputValue = value
  72. })
  73. .defaultFocus(true) // 該TextInput組件為頁面的初始默認焦點
  74. Button('Group3')
  75. .width(165)
  76. .height(40)
  77. .fontColor(Color.White)
  78. Row({ space: 5 }) {
  79. Button()
  80. .width(80)
  81. .height(40)
  82. .fontColor(Color.White)
  83. Button()
  84. .width(80)
  85. .height(40)
  86. .fontColor(Color.White)
  87. }
  88. Button()
  89. .width(165)
  90. .height(40)
  91. .fontColor(Color.White)
  92. Row({ space: 5 }) {
  93. Button()
  94. .width(80)
  95. .height(40)
  96. .fontColor(Color.White)
  97. Button()
  98. .width(80)
  99. .height(40)
  100. .fontColor(Color.White)
  101. }
  102. Button()
  103. .width(165)
  104. .height(40)
  105. .fontColor(Color.White)
  106. Row({ space: 5 }) {
  107. Button()
  108. .width(80)
  109. .height(40)
  110. .fontColor(Color.White)
  111. Button()
  112. .width(80)
  113. .height(40)
  114. .fontColor(Color.White)
  115. }
  116. }.borderWidth(2).borderColor(Color.Orange).borderStyle(BorderStyle.Dashed)
  117. .tabIndex(3) // 該Column組件為按TAB鍵走焦的第三個獲焦的組件
  118. }.alignItems(VerticalAlign.Top)
  119. }
  120. }
  121. }

示意圖:

首次按下TAB鍵,焦點切換到defaultFocus綁定的組件上:

第二次按TAB鍵,焦點切換到tabIndex(1)的容器上,且自動走到其內(nèi)部的groupDefaultFocus綁定的組件上:

第三次按TAB鍵,焦點切換到tabIndex(2)的容器上,且自動走到其內(nèi)部的groupDefaultFocus綁定的組件上:

第四次按TAB鍵,焦點切換到tabIndex(3)的容器上,且自動走到其內(nèi)部的groupDefaultFocus綁定的組件上:

點擊綁定了focusOnTouch的組件,組件自身獲焦:

示例2

focusControl.requestFocus示例代碼:

使用focusContrl.requestFocus接口使指定組件獲取焦點。

  1. // requestFocus.ets
  2. import prompt from '@ohos.prompt'
  3. @Entry
  4. @Component
  5. struct RequestFocusExample {
  6. @State idList: string[] = ['A', 'B', 'C', 'D', 'E', 'F', 'LastPageId']
  7. @State selectId: string = 'LastPageId'
  8. build() {
  9. Column({ space:20 }){
  10. Row({space: 5}) {
  11. Button("id: " + this.idList[0] + " focusable(false)")
  12. .width(200).height(70).fontColor(Color.White)
  13. .key(this.idList[0])
  14. .focusable(false)
  15. Button("id: " + this.idList[1])
  16. .width(200).height(70).fontColor(Color.White)
  17. .key(this.idList[1])
  18. }
  19. Row({space: 5}) {
  20. Button("id: " + this.idList[2])
  21. .width(200).height(70).fontColor(Color.White)
  22. .key(this.idList[2])
  23. Button("id: " + this.idList[3])
  24. .width(200).height(70).fontColor(Color.White)
  25. .key(this.idList[3])
  26. }
  27. Row({space: 5}) {
  28. Button("id: " + this.idList[4])
  29. .width(200).height(70).fontColor(Color.White)
  30. .key(this.idList[4])
  31. Button("id: " + this.idList[5])
  32. .width(200).height(70).fontColor(Color.White)
  33. .key(this.idList[5])
  34. }
  35. Row({space: 5}) {
  36. Select([{value: this.idList[0]},
  37. {value: this.idList[1]},
  38. {value: this.idList[2]},
  39. {value: this.idList[3]},
  40. {value: this.idList[4]},
  41. {value: this.idList[5]},
  42. {value: this.idList[6]}])
  43. .value(this.selectId)
  44. .onSelect((index: number) => {
  45. this.selectId = this.idList[index]
  46. })
  47. Button("RequestFocus")
  48. .width(200).height(70).fontColor(Color.White)
  49. .onClick(() => {
  50. var res = focusControl.requestFocus(this.selectId) // 使選中的this.selectId的組件獲焦
  51. if (res) {
  52. prompt.showToast({message: 'Request success'})
  53. } else {
  54. prompt.showToast({message: 'Request failed'})
  55. }
  56. })
  57. }
  58. }.width('100%').margin({ top:20 })
  59. }
  60. }

示意圖:

按下TAB鍵,激活焦點態(tài)顯示。

申請不存在的組件獲焦:

申請不可獲焦的組件獲焦:

申請存在且可獲焦的組件獲焦:

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號