W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
自定義組件的走焦效果,可設(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 說明: 僅在組件可點擊時才能正常獲取焦點。 |
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)前僅支持在真機上顯示運行效果。
defaultFocus/groupDefaultFocus/focusOnTouch示例代碼:
defaultFocus可以使綁定的組件成為頁面創(chuàng)建后首次獲焦的焦點。groupDefaultFocus可以使綁定的組件成為tabIndex容器創(chuàng)建后首次獲焦的焦點。focusOnTouch可以使綁定的組件點擊后立即獲焦。
- // focusTest.ets
- @Entry
- @Component
- struct FocusableExample {
- @State inputValue: string = ''
- build() {
- Scroll() {
- Row({ space: 20 }) {
- Column({ space: 20 }) {
- Column({ space: 5 }) {
- Button('Group1')
- .width(165)
- .height(40)
- .fontColor(Color.White)
- .focusOnTouch(true) // 該Button組件點擊后可獲焦
- Row({ space: 5 }) {
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- .focusOnTouch(true) // 該Button組件點擊后可獲焦
- }
- Row({ space: 5 }) {
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- }
- }.borderWidth(2).borderColor(Color.Red).borderStyle(BorderStyle.Dashed)
- .tabIndex(1) // 該Column組件為按TAB鍵走焦的第一個獲焦的組件
- Column({ space: 5 }) {
- Button('Group2')
- .width(165)
- .height(40)
- .fontColor(Color.White)
- Row({ space: 5 }) {
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- .groupDefaultFocus(true) // 該Button組件上級Column組件獲焦時獲焦
- }
- Row({ space: 5 }) {
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- }
- }.borderWidth(2).borderColor(Color.Green).borderStyle(BorderStyle.Dashed)
- .tabIndex(2) // 該Column組件為按TAB鍵走焦的第二個獲焦的組件
- }
- Column({ space: 5 }) {
- TextInput({placeholder: 'input', text: this.inputValue})
- .onChange((value: string) => {
- this.inputValue = value
- })
- .defaultFocus(true) // 該TextInput組件為頁面的初始默認焦點
- Button('Group3')
- .width(165)
- .height(40)
- .fontColor(Color.White)
- Row({ space: 5 }) {
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- }
- Button()
- .width(165)
- .height(40)
- .fontColor(Color.White)
- Row({ space: 5 }) {
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- }
- Button()
- .width(165)
- .height(40)
- .fontColor(Color.White)
- Row({ space: 5 }) {
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- Button()
- .width(80)
- .height(40)
- .fontColor(Color.White)
- }
- }.borderWidth(2).borderColor(Color.Orange).borderStyle(BorderStyle.Dashed)
- .tabIndex(3) // 該Column組件為按TAB鍵走焦的第三個獲焦的組件
- }.alignItems(VerticalAlign.Top)
- }
- }
- }
示意圖:
首次按下TAB鍵,焦點切換到defaultFocus綁定的組件上:
第二次按TAB鍵,焦點切換到tabIndex(1)的容器上,且自動走到其內(nèi)部的groupDefaultFocus綁定的組件上:
第三次按TAB鍵,焦點切換到tabIndex(2)的容器上,且自動走到其內(nèi)部的groupDefaultFocus綁定的組件上:
第四次按TAB鍵,焦點切換到tabIndex(3)的容器上,且自動走到其內(nèi)部的groupDefaultFocus綁定的組件上:
點擊綁定了focusOnTouch的組件,組件自身獲焦:
focusControl.requestFocus示例代碼:
使用focusContrl.requestFocus接口使指定組件獲取焦點。
- // requestFocus.ets
- import prompt from '@ohos.prompt'
- @Entry
- @Component
- struct RequestFocusExample {
- @State idList: string[] = ['A', 'B', 'C', 'D', 'E', 'F', 'LastPageId']
- @State selectId: string = 'LastPageId'
- build() {
- Column({ space:20 }){
- Row({space: 5}) {
- Button("id: " + this.idList[0] + " focusable(false)")
- .width(200).height(70).fontColor(Color.White)
- .key(this.idList[0])
- .focusable(false)
- Button("id: " + this.idList[1])
- .width(200).height(70).fontColor(Color.White)
- .key(this.idList[1])
- }
- Row({space: 5}) {
- Button("id: " + this.idList[2])
- .width(200).height(70).fontColor(Color.White)
- .key(this.idList[2])
- Button("id: " + this.idList[3])
- .width(200).height(70).fontColor(Color.White)
- .key(this.idList[3])
- }
- Row({space: 5}) {
- Button("id: " + this.idList[4])
- .width(200).height(70).fontColor(Color.White)
- .key(this.idList[4])
- Button("id: " + this.idList[5])
- .width(200).height(70).fontColor(Color.White)
- .key(this.idList[5])
- }
- Row({space: 5}) {
- Select([{value: this.idList[0]},
- {value: this.idList[1]},
- {value: this.idList[2]},
- {value: this.idList[3]},
- {value: this.idList[4]},
- {value: this.idList[5]},
- {value: this.idList[6]}])
- .value(this.selectId)
- .onSelect((index: number) => {
- this.selectId = this.idList[index]
- })
- Button("RequestFocus")
- .width(200).height(70).fontColor(Color.White)
- .onClick(() => {
- var res = focusControl.requestFocus(this.selectId) // 使選中的this.selectId的組件獲焦
- if (res) {
- prompt.showToast({message: 'Request success'})
- } else {
- prompt.showToast({message: 'Request failed'})
- }
- })
- }
- }.width('100%').margin({ top:20 })
- }
- }
示意圖:
按下TAB鍵,激活焦點態(tài)顯示。
申請不存在的組件獲焦:
申請不可獲焦的組件獲焦:
申請存在且可獲焦的組件獲焦:
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: