HarmonyOS 请问如何令一个输入框自动失焦,并自动收起键盘

HarmonyOS
2024-12-18 17:33:09
1078浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

失焦:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-arkui-uicontext-V5#clearfocus12

聚焦:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-arkui-uicontext-V5#requestfocus12

demo:

export interface routerParam {
  pageId: string
}

@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  @State isFocusable: boolean = true
  private controller: TextInputController = new TextInputController()
  @State routerParams: routerParam = { pageId: "0" }

  build() {
    Column() {
      TextInput({ text: $$this.message, placeholder: "请输入...", controller: this.controller })
        .placeholderColor("#D4D3D1")
        .backgroundColor('#ffcf2424')
        .width(260)
        .borderRadius(8)
        .id("textInput1")
        .margin({ top: 10, bottom: 10 })
        .defaultFocus(this.routerParams.pageId === "1")
        .focusable(this.isFocusable)
      Text().id('textInput2')
      Button("聚焦")
        .width(200)
        .height(45)
        .fontSize(28)
        .type(ButtonType.Normal)
        .border({ width: 1, color: Color.White, radius: 8 })
        .margin({ top: 50, bottom: 60 })
        .onClick(() => {
          this.isFocusable = true
          let res = focusControl.requestFocus('textInput1')
          // this.getUIContext().getFocusController().requestFocus("textInput1")
        })

      Button("失焦")
        .width(200)
        .height(45)
        .fontSize(28)
        .type(ButtonType.Normal)
        .border({ width: 1, color: Color.White, radius: 8 })
        .margin({ top: 50, bottom: 60 })
        .onClick(() => {
          this.isFocusable = false
          // this.getUIContext().getFocusController().clearFocus()
        })
    }
    .height('100%')
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
分享
微博
QQ
微信
回复
2024-12-18 19:42:14
相关问题
H5页面输入框自动弹起软键盘
2759浏览 • 1回复 待解决
HarmonyOS 输入框自动避让偏移值
540浏览 • 1回复 待解决
HarmonyOS 键盘遮挡输入框
700浏览 • 1回复 待解决
js如何清空一个input 输入框的内容
8661浏览 • 2回复 待解决
HarmonyOS 如何控制输入框弹出键盘
959浏览 • 1回复 待解决
HarmonyOS 输入框屏蔽系统键盘
627浏览 • 1回复 待解决