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)
  }
}
分享
微博
QQ
微信
回复
2024-12-18 17:32:40
相关问题
HarmonyOS 如何取消TextInput自动聚焦
887浏览 • 1回复 待解决
Textinput是否支持自动聚焦
574浏览 • 1回复 待解决
TextInput输入行满无法自动换行
823浏览 • 1回复 待解决
TextInput ,TextArea无法设置字体间距
627浏览 • 1回复 待解决
TextInput在聚焦如何使光标回到起点
2042浏览 • 1回复 待解决
TextInput在聚焦如何让光标回到起点
2705浏览 • 3回复 待解决
HarmonyOS flutter组件无法自动下载
401浏览 • 1回复 待解决
TextInput如何取消默认聚焦效果
2853浏览 • 1回复 待解决
HarmonyOS Navigation无法正确显示
291浏览 • 1回复 待解决
HarmonyOS heif图片无法显示
385浏览 • 1回复 待解决
DevEco Studio 无法自动配置gradle
10385浏览 • 2回复 已解决