HarmonyOS TextArea控件,当设置的text高度超过最大值时,如何自动滑动到底部?

如题:HarmonyOS TextArea控件,当设置的text高度超过最大值时,如何自动滑动到底部?

阅读 540
1 个回答

获取输入的内容的长度,然后将光标通过接口caretPosition()将光标跳到最后一行,即可到最后一行。

代码示例

// xxx.ets
@Entry
@Component
struct TextAreaExample {
  @State text: string = ''
  @State positionInfo: CaretOffset = { index: 0, x: 0, y: 0 }
  controller: TextAreaController = new TextAreaController()
  @State maxLength: number= 0

  build() {
    Column() {
      TextArea({
        text: this.text,
        placeholder: 'The text area can hold an unlimited amount of text. input your word...',
        controller: this.controller
      })
        .placeholderFont({ size: 16, weight: 400 })
        .width(336)
        .height(56)
        .margin(20)
        .fontSize(16)
        .fontColor('#182431')
        .backgroundColor('#FFFFFF')
        .onChange((value: string) => {
          let Length: number= value.length;
          if(this.maxLength< Length){
            this.maxLength = Length
            console.log('length='+this.maxLength)
          }
          this.text = value
        })
      Text(this.text)

      Button('跳转到最后一行').onClick((event: ClickEvent) => {
        this.controller.caretPosition(this.maxLength)
      })
    }.width('100%').height('100%').backgroundColor('#F1F3F5')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进