W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
焦點(diǎn)事件指頁面焦點(diǎn)在可獲焦組件間移動時(shí)觸發(fā)的事件,組件可使用焦點(diǎn)事件來處理相關(guān)邏輯。
從API Version 8開始支持。后續(xù)版本如有新增內(nèi)容,則采用上角標(biāo)單獨(dú)標(biāo)記該內(nèi)容的起始版本。
目前僅支持通過外接鍵盤的tab鍵、方向鍵觸發(fā)。
存在默認(rèn)交互邏輯的組件例如Button、TextInput等,默認(rèn)即為可獲焦,Text、Image等組件則默認(rèn)狀態(tài)為不可獲焦,不可獲焦?fàn)顟B(tài)下,無法觸發(fā)焦點(diǎn)事件,需要設(shè)置focusable屬性為true才可觸發(fā)。
名稱 | 支持冒泡 | 功能描述 |
---|---|---|
onFocus(event: () => void) | 否 | 當(dāng)前組件獲取焦點(diǎn)時(shí)觸發(fā)的回調(diào)。 |
onBlur(event:() => void) | 否 | 當(dāng)前組件失去焦點(diǎn)時(shí)觸發(fā)的回調(diào)。 |
- // xxx.ets
- @Entry
- @Component
- struct FocusEventExample {
- @State oneButtonColor: string = '#FFC0CB'
- @State twoButtonColor: string = '#87CEFA'
- @State threeButtonColor: string = '#90EE90'
- build() {
- Column({ space: 20 }) {
- // 通過外接鍵盤的上下鍵可以讓焦點(diǎn)在三個(gè)按鈕間移動,按鈕獲焦時(shí)顏色變化,失焦時(shí)變回原背景色
- Button('First Button')
- .backgroundColor(this.oneButtonColor)
- .width(260)
- .height(70)
- .fontColor(Color.Black)
- .focusable(true)
- .onFocus(() => {
- this.oneButtonColor = '#FF0000'
- })
- .onBlur(() => {
- this.oneButtonColor = '#FFC0CB'
- })
- Button('Second Button')
- .backgroundColor(this.twoButtonColor)
- .width(260)
- .height(70)
- .fontColor(Color.Black)
- .focusable(true)
- .onFocus(() => {
- this.twoButtonColor = '#FF0000'
- })
- .onBlur(() => {
- this.twoButtonColor = '#87CEFA'
- })
- Button('Third Button')
- .backgroundColor(this.threeButtonColor)
- .width(260)
- .height(70)
- .fontColor(Color.Black)
- .focusable(true)
- .onFocus(() => {
- this.threeButtonColor = '#FF0000'
- })
- .onBlur(() => {
- this.threeButtonColor = '#90EE90'
- })
- }.width('100%').margin({ top: 20 })
- }
- }
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: