文档开发者中心
我的
|

HarmonyOS 5

按钮内子控件如何动态更新

收起
自动换行
深色代码主题
复制
Button({ type: ButtonType.Normal, stateEffect: true }){
  Row() {
    Image($r('app.media.login_checkbox_normal')).width(20).height(20)
    Text($r('app.string.login_auto')).fontSize($r('app.float.button_size')).fontColor($r('app.color.text_grey_1')).margin({ left: 10 })
  }
}
.backgroundColor(Color.White)

更新里面的图片和文字

点赞
收藏
回复
2
分享
举报
浏览225 发布于2024-02-20 07:16甘肃
全部评论
最多点赞
最新发布
最早发布
|
技术支持

可以参考以下案例:

收起
自动换行
深色代码主题
复制
//按钮内子控件如何动态更新
@Entry
@Component
struct Index {
  @State text:string | ResourceStr = '这是一张图片'
  @State img:Resource = $r('app.media.img')
  @State flag: boolean = false
  build(){
    Column({space: 20}){
      Text('按钮内子控件如何动态更新')
        .fontSize(24)
        .fontWeight(FontWeight.Bold)
        .fontColor(Color.Blue)

      Button({ type: ButtonType.Normal, stateEffect: true }){
        Row() {
          Text(this.text) //按钮内文本控件
            .fontSize(16)
            .fontColor(Color.Yellow)
            .fontWeight(FontWeight.Bold)
            .width('40%')

          Image(this.img) //按钮内图片控件
            .width(200)
            .height(200)
        }
        .width('90%')
        .height(200)
      }

      //动态更新文本控件以及图片控件的内容
      Button('更新图片文字')
        .onClick(()=>{
          if(!this.flag){
            this.text = '这是一张新的图片'
            this.img = $r('app.media.img')
            this.flag = !this.flag
          }else {
            this.text = '一张新的图片'
            this.img = $r('app.media.img_1')
            this.flag = !this.flag
          }

      })

    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}
展开全部
2楼编辑于2024-02-29 02:05 来自广东

cke_122.pngcke_283.png

收起
自动换行
深色代码主题
复制
@Entry
@Component
struct test {
  @State text:string = '测试'
  @State img:Resource = $r('app.media.ic_widget')
  build(){
    Column(){
      Button({ type: ButtonType.Normal, stateEffect: true }){
        Row() {
          Image(this.img).width(20).height(20)
          Text(this.text).fontSize(30).fontColor(Color.Gray).margin({ left: 10 })
        }
      }
      .backgroundColor(Color.White)

      Button('更新图标').onClick(()=>{
        this.img = $r('app.media.icon_2')
      })
      Button('更新文字').onClick(()=>{
        this.text = '李四'
      })
    }
  }
}
1楼回复于2024-02-21 03:14 来自北京
写回答
新增插入模板功能
一键使用模板,快速填写内容,轻松发帖~
知道了
  • 为了保障您的信息安全,请勿上传您的敏感个人信息(如您的密码等信息)和您的敏感资产信息(如关键源代码、签名私钥、调试安装包、业务日志等信息),且您需自行承担由此产生的信息泄露等安全风险。
  • 如您发布的内容为转载内容,请注明内容来源。

我要提问题

了解社区公约,与您携手共创和谐专业的开发者社区。