HarmonyOS 弹出一个自定义弹窗,里面包含一个多行输入框,怎么让弹窗弹出的时候,输入框自动获取焦点?

HarmonyOS 弹出一个自定义弹窗,里面包含一个多行输入框,怎么让弹窗弹出的时候,输入框自动获取焦点?

HarmonyOS
2024-11-27 08:44:07
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

在输入框上加上defaultFocus属性,即可在弹窗弹出时,输入框自动获焦。

@CustomDialog 
export struct BottomCommentBoxDialog { 
  @State placeholder: string = "我来说两句" 
  onComment?: (content:string )=>void 
  @State content: string = "" 
  controller: CustomDialogController = new CustomDialogController({ 
    builder: BottomCommentBoxDialog({}), 
  }) 
  editController: TextAreaController = new TextAreaController() 
  build() { 
    Row() { 
      TextArea({ text: this.content, placeholder: this.placeholder, controller: this.editController }) 
        .fontSize(16) 
        .fontFamily("normal") 
        .fontColor(Color.Black) 
        .placeholderColor(Color.Gray) 
        .placeholderFont({ family:"normal" }) 
        .onChange((value: string) => { 
          this.content = value 
        }) 
        .maxLines(3) 
        .constraintSize({ 
          minHeight:60, 
          maxHeight:110 
        }) 
        .layoutWeight(1) 
          // 自动获焦 
        .defaultFocus(true) 
      Text("发布").fontColor(this.content.trim().length == 0 ? Color.Gray : Color.Red).fontSize(17).fontFamily("normal").padding(13).onClick(()=>{ 
        if(this.onComment){ 
          this.onComment(this.content) 
        } 
      }) 
    }.padding({ 
      left: 13, 
      top: 10, 
      bottom: 30 
    }).width('100%').backgroundColor(Color.White).borderRadius(0) 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-11-27 14:44:00
相关问题
HarmonyOS 输入框获取焦点后无法弹出
764浏览 • 1回复 待解决
js如何清空一个input 输入框内容
8674浏览 • 2回复 待解决
HarmonyOS 如何控制输入框弹出键盘
985浏览 • 1回复 待解决
HarmonyOS 自定义键盘输入框焦点问题
959浏览 • 1回复 待解决
HarmonyOS 修改输入框焦点
739浏览 • 1回复 待解决
HarmonyOS 自定义键盘不能顶起输入框
1243浏览 • 1回复 待解决
获取输入框输入内容
478浏览 • 2回复 待解决