HarmonyOS 如何控制自定义键盘获取焦点和失去焦点

如何控制自定义键盘获取焦点和失去焦点

HarmonyOS
2024-12-20 16:29:08
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

参考demo:

@Entry
@Component
struct TextInputExample {
  controller: TextInputController = new TextInputController()
  @State inputValue: string = ""

  // 自定义键盘组件
  @Builder CustomKeyboardBuilder() {
    Column() {
      Grid() {
        ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item:number|string) => {
          GridItem() {
            Button(item + "")
              .width(110).onClick(() => {
              this.inputValue += item
            })
          }
        })
      }.maxCount(3).columnsGap(10).rowsGap(10).padding(5)
    }.backgroundColor(Color.Gray)
  }

  build() {
    Column() {
      Button('拉起键盘').onClick(() => {
        focusControl.requestFocus('txt1')
      })
        .id('btn1')
      Button('收起键盘').onClick(() => {
        focusControl.requestFocus('btn1')
      })
      TextInput({ controller: this.controller, text: this.inputValue })// 绑定自定义键盘
        .customKeyboard(this.CustomKeyboardBuilder())
        .margin(10)
        .border({ width: 1 })
        .height('48vp')
        .id('txt1')
      TextInput({ placeholder: '没有关联自定义键盘' })
    }
  }
}
分享
微博
QQ
微信
回复
2024-12-20 18:57:02
相关问题
HarmonyOS Image获取焦点失去焦点失效
218浏览 • 1回复 待解决
ets的text input手动控制获得失去焦点
4938浏览 • 1回复 待解决
HarmonyOS 自定义键盘输入框焦点问题
487浏览 • 1回复 待解决
HarmonyOS InputType失去焦点禁止交互
244浏览 • 1回复 待解决
如何判断音频焦点获取丢失?
6426浏览 • 1回复 待解决
HarmonyOS TextArea获取焦点
173浏览 • 1回复 待解决