HarmonyOS 如何手动打开/关闭系统软件盘?

HarmonyOS
2024-12-18 15:30:21
614浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

可以通过focusControl.requestFocus控制输入框获焦,组件获焦后会自动弹起软键盘。

1.通过让TextInput失焦的方法,让软键盘收起,比如通过让别的组件获焦而使当前组件失焦,可以将焦点转移给其他组件, 例如,提供一个button组件,并设置点击时可获焦,当点击按钮时,可让TextInput失焦,软键盘收起

2.使用focusContrl.requestFocus接口使指定组件获取焦点。可参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-focus-V5

TextInput:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-textinput-V5

注意Text、Image等组件则默认状态为不可获焦。不可获焦状态下,无法触发焦点事件。

// xxx.ets
@CustomDialog
struct CustomDialogExample {
  private focusKey = 'name_input'; // 输入框焦点
  controller?: CustomDialogController
  cancel: () => void = () => {
  }
  confirm: () => void = () => {
  }

  // onPageHide(): void {
  //   focusControl.requestFocus(this.focusKey)
  // }

  build() {
    Column() {
      Text('这是自定义弹窗')
        .fontSize(30)
        .height(100)

      Text('昵称')
        .fontSize(20)
        .margin({ top: 10, bottom: 10 })
      // .key(this.focusKey)
      // .id(this.focusKey)

      TextInput({ placeholder: '输入昵称',text:'', })
        .height(60)
        .width('90%')
        .defaultFocus(true)

      Button('测试')
        .key(this.focusKey)


      Button('点我关闭弹窗')
        .onClick(() => {
          if (this.controller != undefined) {
            this.controller.close()
          }
        })
        .margin(20)
    }
  }
}
@Entry
@Component
struct CustomDialogUser {
  dialogController: CustomDialogController | null = new CustomDialogController({
    builder: CustomDialogExample({
      cancel: ()=> { this.onCancel() },
      confirm: ()=> { this.onAccept() }
    }),
    cancel: this.existApp,
    autoCancel: true,
    onWillDismiss:(dismissDialogAction: DismissDialogAction)=> {
      console.info("reason=" + JSON.stringify(dismissDialogAction.reason))
      console.log("dialog onWillDismiss")
      if (dismissDialogAction.reason == DismissReason.PRESS_BACK) {
        dismissDialogAction.dismiss()
      }
      if (dismissDialogAction.reason == DismissReason.TOUCH_OUTSIDE) {
        dismissDialogAction.dismiss()
      }
    },
    alignment: DialogAlignment.Center,
    offset: { dx: 0, dy: -20 },
    customStyle: false,
    cornerRadius: 20,
    width: 300,
    height: 400,
    borderWidth: 1,
    borderStyle: BorderStyle.Dashed,//使用borderStyle属性,需要和borderWidth属性一起使用
    borderColor: Color.Blue,//使用borderColor属性,需要和borderWidth属性一起使用
    backgroundColor: Color.White,
    shadow: ({ radius: 20, color: Color.Grey, offsetX: 50, offsetY: 0}),
  })
  // 在自定义组件即将析构销毁时将dialogController置空
  aboutToDisappear() {
    this.dialogController = null // 将dialogController置空
  }

  onCancel() {
    console.info('Callback when the first button is clicked')
  }

  onAccept() {
    console.info('Callback when the second button is clicked')
  }

  existApp() {
    console.info('Click the callback in the blank area')
  }

  build() {
    Column() {
      Button('click me')
        .onClick(() => {
          if (this.dialogController != null) {
            this.dialogController.open()
          }
        }).backgroundColor(0x317aff)
    }.width('100%').margin({ top: 5 })
  }
}
  • 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.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
分享
微博
QQ
微信
回复
2024-12-18 16:33:44
相关问题
HarmonyOS 如何获取系统软件版本
812浏览 • 1回复 待解决
鸿蒙TextField 软件不弹出
6616浏览 • 2回复 待解决
HarmonyOS如何控制打开关闭手电筒
1333浏览 • 1回复 待解决
【ets】switch开关打开后会自动关闭
4439浏览 • 1回复 待解决
HarmonyOS 如何打开系统相册页面
1189浏览 • 1回复 待解决
弹窗打开关闭动画是否支持自定义
3367浏览 • 1回复 待解决
HarmonyOS 打开系统的位置开关
1212浏览 • 1回复 待解决
HarmonyOS 如何手动重启APP
551浏览 • 1回复 待解决
软件下载问题 软件下载问题!?!
6454浏览 • 1回复 待解决