HarmonyOS TextArea显示时无法自动聚焦

HarmonyOS
2024-12-18 15:29:33
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
put_get

通过requestFocus获取焦点 参考demo如下:

import inputMethod from ‘@ohos.inputMethod’;

@Entry
@Component
export struct AsrPage {
  @State message: string = ‘Hello World’
  @State show: boolean = false
  inputMethodController = inputMethod.getController();
  controller: TextInputController = new TextInputController()
  inputId:string = ‘ceshi’
  aboutToAppear(): void {
    let textConfig: inputMethod.TextConfig = {
      inputAttribute: {
        textInputType: 0,
        enterKeyType: 1
      }
    }
    this.inputMethodController.attach(false, textConfig)
      .then(() => {
      })
      .catch((reason: ESObject) => {
      })
  }
  build() {
    NavDestination() {
      Column() {
        TextArea({
          placeholder: ‘’,
          text: ‘’
        })
          .width(‘100%’)
        .height(26)
          .defaultFocus(true)
          .visibility(this.show ? Visibility.Visible : Visibility.None)
          .borderColor(’#ff0000’)
        .backgroundColor(’#ff0000’)
        .id(this.inputId)

        Button(‘打开’).onClick(() => {
          this.show = true
          this.controller.caretPosition(this.message.length)
          focusControl.requestFocus(this.inputId)
          this.inputMethodController.showTextInput().then(() => {
          }).catch((reason: ESObject) => {

          })

        })
        Button(‘关闭’).onClick(() => {
          this.show = true
          this.inputMethodController.hideTextInput().then(() => {
          }).catch((reason: ESObject) => {
          })

        })
      }.justifyContent(FlexAlign.Center).width(‘100%’).height(‘100%’)
    }.hideTitleBar(true)
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
分享
微博
QQ
微信
回复
2024-12-18 17:32:40
相关问题
HarmonyOS 如何取消TextInput自动聚焦
1246浏览 • 1回复 待解决
Textinput是否支持自动聚焦
831浏览 • 1回复 待解决
TextInput输入行满无法自动换行
1231浏览 • 1回复 待解决
TextInput ,TextArea无法设置字体间距
965浏览 • 1回复 待解决
TextInput在聚焦如何让光标回到起点
3153浏览 • 3回复 待解决
TextInput在聚焦如何使光标回到起点
2345浏览 • 1回复 待解决
HarmonyOS flutter组件无法自动下载
836浏览 • 1回复 待解决
HarmonyOS TextInput组件无法自动获取焦点
1022浏览 • 1回复 待解决
HarmonyOS TextArea获取焦点
538浏览 • 1回复 待解决
HarmonyOS heif图片无法显示
824浏览 • 1回复 待解决
HarmonyOS Navigation无法正确显示
663浏览 • 1回复 待解决
TextInput如何取消默认聚焦效果
3339浏览 • 1回复 待解决