HarmonyOS鸿蒙Next中RichEditor如何设置最大的字符输入限制?比如设置最多输入100个字符,输入到100个后,就不能继续输入了。

HarmonyOS鸿蒙Next中RichEditor如何设置最大的字符输入限制?比如设置最多输入100个字符,输入到100个后,就不能继续输入了。 RichEditor如何设置最大的字符输入限制啊?

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

3 回复

目前鸿蒙没有给出相关接口限制字符数目,可以参考官方给的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
        })
    }
  }
}

更多关于HarmonyOS鸿蒙Next中RichEditor如何设置最大的字符输入限制?比如设置最多输入100个字符,输入到100个后,就不能继续输入了。的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,RichEditor组件可以通过设置maxLength属性来限制最大字符输入数。例如,设置最多输入100个字符,可以在布局文件中直接配置maxLength="100",或者在代码中动态设置richEditor.setMaxLength(100)。当输入字符数达到100时,用户将无法继续输入。

在HarmonyOS鸿蒙Next中,可以通过RichEditor的setMaxLength方法来设置最大字符输入限制。具体代码如下:

RichEditor richEditor = (RichEditor) findComponentById(ResourceTable.Id_rich_editor);
richEditor.setMaxLength(100);

此代码将RichEditor的最大输入字符数限制为100个字符。当用户输入达到100个字符后,编辑器将阻止进一步输入。

回到顶部