HarmonyOS TextArea获取焦点

有一个页面由List组成,里面有很多ItemView,然后我的这个页面 还有一个评论输入组件,起名为:CommentPublishBar

代码如下:

@Preview
@Component
export default struct CommentPublishBar {
  @Prop keyboardShow: boolean = false //软键盘当前是否展示
  @Prop showHeight: number = getScreenWidth()
  @State textAreaHeight: number = 0;
  @Prop postData: PostDataBean
  @Prop rid: number = 0;
  @State content: string = ''
  commentPublisher: CommentPublisher = new CommentPublisher()
  @Link commentList: Comment[]

  textInputController: TextAreaController = new TextAreaController()

  aboutToAppear(): void {
  }

  build() {
    Column() {
      Column()
        .backgroundColor(Color.Transparent)
        .width('100%')
        .height(this.keyboardShow ? px2vp(this.showHeight) - 50 - (this.textAreaHeight - 38) : 0)
        .onClick(() => {
          this.closePublish(false)
        })
      Row() {
        TextArea({
          placeholder: '说亿点好听的',
          controller: this.textInputController,
          text: this.content
        })
          .width(this.keyboardShow ? px2vp(getScreenWidth()) - 96 : px2vp(getScreenWidth()) - 32)
          .backgroundColor($r('app.color.BG_2'))
          .borderRadius(18)
          .margin({ left: 16, right: 16 })
          .fontColor($r('app.color.CT_2'))
          .placeholderColor($r('app.color.Text_4'))
          .fontSize(15)
          .padding({ left: 12 })
          .constraintSize({
            minHeight: 38
          })
          .onChange((value) => {
            this.content = value
          })
          .onAreaChange((oldValue, newValue) => {
            if (typeof newValue.height === 'number') {
              this.textAreaHeight = newValue.height
            }
          }
          )
          .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM])

        if (this.keyboardShow) {
          Text('发布')
            .width(58)
            .height(32)
            .borderRadius(4)
            .backgroundColor($r('app.color.CM_Blue'))
            .textAlign(TextAlign.Center)
            .fontColor($r('app.color.CW'))
            .fontSize(14)
            .onClick(() => {
              this.commentPublisher.localId = systemDateTime.getTime().toString()
              ToastUtils.showToast('评论发送中')
              this.commentPublisher.checkReviewContent(this.postData, this.content, this.postData.id, this.rid, (data: PostReviewResult) => {
                let list: Comment[] = []
                list.push(data.comment)
                for (let item of this.commentList) {
                  list.push(item)
                }
                this.commentList = list
              })
              this.closePublish(true)
            })
        }
      }.width('100%')
      .alignItems(VerticalAlign.Bottom)
      .padding({ top: 6, bottom: 6 })
      .backgroundColor($r('app.color.CB'))
    }
  }

  closePublish(needClearContent: boolean) {
    this.rid = 0
    inputMethod.getInputMethodController().stopInputSession()
    this.getUIContext().getFocusController().clearFocus()
    if (needClearContent) {
      this.content = ''
    }
  }

  getTextAreaHeight() {
  }
}

希望点击itemView可以和直接点击TextArea达到同样的效果,唤起软键盘,TextArea获取焦点,应该怎么做

HarmonyOS
2025-01-09 14:42:53
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

可以参考一下下面的demo

@Entry
@Component
struct TextAreaDemo {
  controller: TextAreaController = new TextAreaController()
  @State text: string = ''
  build() {
    Column() {
      Text('光标测试')
      Scroll() {
        Column() {

          Row() {
            TextArea({ placeholder: '默认光标1 ' ,controller: this.controller, text: this.text})
              .margin(30)
              .defaultFocus(true)
              .id('focus1')
          }
          .backgroundColor(Color.Orange)
          .focusOnTouch(true)
          .onClick(()=>{
            focusControl.requestFocus('focus1') // 使选中的Id的组件获焦,这里要注意获焦的id要与组件的id保持一致
          })


          Row() {
            TextArea({ placeholder: '默认光标2 ' ,controller: this.controller, text: this.text})
              .margin(30)
              .id('focus2')
          }
          .backgroundColor(Color.Gray)
          .focusOnTouch(true)
          .onClick(()=>{
            focusControl.requestFocus('focus2') // 使选中的Id的组件获焦,这里要注意获焦的id要与组件的id保持一致
          })
        }
      }
      .height('100%')
      .width('100%')
    }
  }
}
分享
微博
QQ
微信
回复
2025-01-09 17:23:35
相关问题
HarmonyOS TextArea组件如何主动获取焦点
107浏览 • 1回复 待解决
HarmonyOS Image获取焦点和失去焦点失效
206浏览 • 1回复 待解决
HarmonyOS 主动获取组件焦点
424浏览 • 1回复 待解决
HarmonyOS 主动获取焦点失败
543浏览 • 1回复 待解决
HarmonyOS 获取焦点api提示异常
301浏览 • 1回复 待解决
HarmonyOS TextInput自动获取焦点问题
405浏览 • 1回复 待解决
TextInput组件获取焦点的几种场景
3238浏览 • 1回复 待解决
focusControl.requestFocus获取焦点的问题
460浏览 • 1回复 待解决
HarmonyOS 输入框获取焦点后无法弹出
355浏览 • 1回复 待解决
如何判断音频焦点获取和丢失?
6401浏览 • 1回复 待解决