获取输入的内容的长度,然后将光标通过接口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') } }
获取输入的内容的长度,然后将光标通过接口caretPosition()将光标跳到最后一行,即可到最后一行。
代码示例