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

HarmonyOS
2024-12-18 17:33:09
浏览
收藏 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)
  }
}
分享
微博
QQ
微信
回复
2024-12-18 19:42:14
相关问题
H5页面输入框自动弹起软键盘
2211浏览 • 1回复 待解决
HarmonyOS 输入框自动避让偏移值
264浏览 • 1回复 待解决
js如何清空一个input 输入框的内容
8187浏览 • 1回复 待解决
HarmonyOS 键盘遮挡输入框
307浏览 • 1回复 待解决
HarmonyOS 如何控制输入框弹出键盘
501浏览 • 1回复 待解决
HarmonyOS 输入框屏蔽系统键盘
284浏览 • 1回复 待解决