HarmonyOS RichEditor如何设置最大的字符输入限制?

比如设置最多输入100个字符,输入到100个后,就不能继续输入了

阅读 687
1 个回答

当前暂无限制最大字符输入数的接口

请参考如下demo:

// 输入框文字校验
inputNumCheck(){
  this.controllerRich.getSpans({
    start: this.start,
    end: this.end}).forEach(item => {
    if (typeof (item as RichEditorImageSpanResult)['imageStyle'] !== 'undefined') {
      this.inputNum++
    } else {
      this.inputNum = (item as RichEditorTextSpanResult).value.length;
    }
  })
}
build() {
  Column() {
    Row(){
      // 输入框
      RichEditor({ controller: this.controllerRich})
        .height($r('app.integer.chat_with_expression_chat_input_height'))
        .layoutWeight(LAYOUT_WEIGHT)
        .borderRadius($r('app.integer.chat_with_expression_chat_border_radius'))
        .backgroundColor($r('app.string.chat_with_expression_input_background'))
        .margin({ top: $r('app.integer.chat_with_expression_express_margin_top') ,left:14})
        .key(this.focusKey)
        .id(this.focusKey)
        .placeholder('平台提倡文明用语,请温柔发言哦~',{font:{size:12},fontColor:'#999999'})
        .defaultFocus(true)
        .aboutToIMEInput((value: RichEditorInsertValue)=>{
          if (this.inputNum >= 50) {
            // 输入字符数超过50个 就禁止输入
            try {
              promptAction.showToast({
                message: '最多只能输入50个汉字哦',
                duration: 2000
              });
            } catch (error) {
              let message = (error as BusinessError).message
              let code = (error as BusinessError).code
              console.error(`showToast args error code is ${code}, message is ${message}`);
            };
            return false
          } else {
            this.inputNumCheck()
          }
          return true
        })
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进