示例参考如下:
@Entry @Component struct SelectedIndex { text: string = 'Hello World'; controller: TextInputController = new TextInputController(); build() { Row() { TextInput({ text: '选中默认文本内容', placeholder: 'input your word...', controller: this.controller }) .width('100%') .defaultFocus(true) // 默认获取焦点 .onFocus(() => { this.controller.setTextSelection(0, this.text.length); // 选择文本区域 }); } .padding({ left: 16, right: 16 }) .width('100%') .height('100%'); } }