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
HarmonyOS
2024-08-10 13:22:14
浏览
收藏 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 }) 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-08-10 18:11:05
相关问题
HarmonyOS TextInput自定义键盘问题
146浏览 • 1回复 待解决
HarmonyOS 自定义弹窗CustomDialog问题
161浏览 • 1回复 待解决
HarmonyOS 自定义弹窗的问题
221浏览 • 1回复 待解决
自定义组件的传值和绑定
605浏览 • 1回复 待解决
基于自定义键盘设置光标位置
175浏览 • 1回复 待解决
HarmonyOS TextInput键盘相关问题咨询
217浏览 • 1回复 待解决
HarmonyOS 自定义Dialog显示问题
91浏览 • 1回复 待解决
TextInput是否能自定义hover效果
1886浏览 • 1回复 待解决
自定义弹窗自定义转场动画
654浏览 • 1回复 待解决
HarmonyOS 引用自定义web的模块问题
73浏览 • 1回复 待解决
自定义弹窗使用相关问题
621浏览 • 1回复 待解决
HarmonyOS 自定义Dialog背景色透明问题
231浏览 • 1回复 待解决
自定义装饰器的使用问题
454浏览 • 1回复 待解决
HarmonyOS 如何自定义BuildMode?
100浏览 • 1回复 待解决
HarmonyOS 如何设置自定义字体
290浏览 • 1回复 待解决
自定义参数BuildProfile的问题汇总
767浏览 • 1回复 待解决