HarmonyOS TextInput绑定自定义键盘问题

键盘响应事件部分的代码:

case KeyType.Value://输入数字或字母 
let startPart = this.inputValue.slice(0,this.controller.getCaretOffset().index) 
let secendPart = this.inputValue.slice(this.controller.getCaretOffset().index,this.inputValue.length) 
this.inputValue = startPart + value + secendPart 
this.controller.caretPosition(this.controller.getCaretOffset().index + 1) 
break 
case KeyType.Del1://删除键 
if (this.inputValue.length > 0 && this.controller.getCaretOffset().index > 0) { 
  let startPart = this.inputValue.slice(0,this.controller.getCaretOffset().index) 
  let secendPart = this.inputValue.slice(this.controller.getCaretOffset().index,this.inputValue.length) 
  this.inputValue = startPart.substring(0, startPart.length - 1) + secendPart 
  this.controller.caretPosition(this.controller.getCaretOffset().index -1) 
} 
break
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
HarmonyOS
2024-08-10 13:22:14
1083浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

目前看下来.应该是光标设置部分存在问题.可以参考如下demo.请参考demo:

@Entry 
@Component 
struct TextInputExample { 
  controller: TextInputController = new TextInputController() 
  @State inputValue: string = "" 
  @State currentCaretOffset: number = 0; 
 
  // 自定义键盘组件 
  @Builder 
  CustomKeyboardBuilder() { 
    Column() { 
      Button('x').onClick(() => { 
        // 关闭自定义键盘 
        this.controller.stopEditing() 
      }) 
      Grid() { 
        ForEach([1, 2, 3, 4, 5, 6, 7, 8, 9, '*', 0, '#'], (item: number | string) => { 
          GridItem() { 
            Button(item + "") 
              .width(110).onClick(() => { 
              let curCaret = this.controller.getCaretOffset(); 
              this.currentCaretOffset = curCaret.index; 
              this.inputValue = this.inputValue.slice(0, this.currentCaretOffset) + item + this.inputValue.slice(this.currentCaretOffset); 
              this.currentCaretOffset++; 
            }) 
          } 
        }) 
      }.maxCount(3).columnsGap(10).rowsGap(10).padding(5) 
    }.backgroundColor(Color.Gray) 
  } 
 
  build() { 
    Column() { 
      TextInput({ controller: this.controller, text: this.inputValue }) 
        .onChange(() => { 
          this.controller.caretPosition(this.currentCaretOffset) 
        })// 绑定自定义键盘 
        .customKeyboard(this.CustomKeyboardBuilder()).margin(10).border({ width: 1 }) 
    } 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-08-10 18:11:05


相关问题
HarmonyOS TextInput自定义键盘问题
1547浏览 • 1回复 待解决
HarmonyOS WebView使用自定义键盘问题
857浏览 • 1回复 待解决
HarmonyOS TextInput调用系统键盘问题
870浏览 • 1回复 待解决
HarmonyOS TextInput自定义键盘
1283浏览 • 1回复 待解决
HarmonyOS 键盘问题
552浏览 • 1回复 待解决
HarmonyOS键盘问题
469浏览 • 1回复 待解决
HarmonyOS web弹起键盘问题
763浏览 • 1回复 待解决
HarmonyOS 自定义键盘
1003浏览 • 1回复 待解决
HarmonyOS Focus进入时弹出键盘问题
746浏览 • 1回复 待解决
HarmonyOS 自定义键盘输入框焦点问题
923浏览 • 1回复 待解决
小程序示例自定义键盘
1064浏览 • 1回复 待解决
HarmonyOS 键盘顶部添加自定义组件
940浏览 • 1回复 待解决
HarmonyOS 数字自定义键盘如何实现
1212浏览 • 1回复 待解决