HarmonyOS 2个TextInput控件,当TextInput输入都有值时,改变Text的背景色?

如题:HarmonyOS 2个TextInput控件,当TextInput输入都有值时,改变Text的背景色?

阅读 573
1 个回答

参考代码:

@Entry
@Component
struct TextInputDemo {
  @State message: string = 'Hello World';
  @State text1: string = ''
  @State text2: string = ''
  @State changeColoor : boolean=false
  controller: TextInputController = new TextInputController()

  build() {
    Column(){
      Text(this.message)
        .id('HelloWorld')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
        .alignRules({
          center: { anchor: '__container__', align: VerticalAlign.Center },
          middle: { anchor: '__container__', align: HorizontalAlign.Center }
        })
        .backgroundColor(this.changeColoor ? Color.Red: '')
      TextInput({ text: this.text1, placeholder: 'input your word1...', controller: this.controller })
        .width('95%')
        .height(40)
        .margin(20)
        .onChange((value: string)=>{
          this.text1 = value
          if(this.text2 && this.text1){
            this.changeColoor = true
          }else{
            this.changeColoor = false
          }

        })
      TextInput({ text: this.text2, placeholder: 'input your word2...', controller: this.controller })
        .width('95%')
        .height(40)
        .margin(20)
        .onChange((value: string)=>{
          this.text2 = value
          if(this.text1 && this.text2){
            this.changeColoor = true
          }else {
            this.changeColoor = false
          }
        })
    }
    .height('100%')
    .width('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进