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() {
  }
}
  • 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.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.

希望点击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%')
    }
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 17:23:35


相关问题
HarmonyOS TextArea组件如何主动获取焦点
557浏览 • 1回复 待解决
HarmonyOS Image获取焦点和失去焦点失效
603浏览 • 1回复 待解决
HarmonyOS 主动获取组件焦点
844浏览 • 1回复 待解决
HarmonyOS 主动获取焦点失败
888浏览 • 1回复 待解决
HarmonyOS 获取焦点api提示异常
723浏览 • 1回复 待解决
HarmonyOS TextInput自动获取焦点问题
803浏览 • 1回复 待解决
HarmonyOS TextInput组件无法自动获取焦点
1022浏览 • 1回复 待解决
TextInput组件获取焦点的几种场景
3948浏览 • 1回复 待解决
focusControl.requestFocus获取焦点的问题
865浏览 • 1回复 待解决
HarmonyOS 输入框获取焦点后无法弹出
762浏览 • 1回复 待解决
如何判断音频焦点获取和丢失?
6776浏览 • 1回复 待解决