HarmonyOS 组件设置onClick事件,如何实现点击组件过程中,组件背景的变化效果

组件设置onClick事件,如何实现点击组件过程中,组件背景的变化效果

HarmonyOS
2024-12-20 16:37:25
1204浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Excelsior_abit

通过设置组件的StateStyle来改变组件的背景,参考代码如下;

@Entry
@Component
struct Index {
  @State isEnable: boolean = true
  @Styles pressedStyles() {
    .backgroundColor(Color.Red)
    .borderRadius(10)
    .borderStyle(BorderStyle.Dashed)
    .borderWidth(2)
    .borderColor("#33000000")
    .width(120)
    .height(30)
    .opacity(1)
  }

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center }) {

      Text("pressed")
        .backgroundColor("#0A59F7")
        .borderRadius(20)
        .borderStyle(BorderStyle.Dotted)
        .borderWidth(2)
        .borderColor(Color.Red)
        .width(100)
        .height(25)
        .opacity(1)
        .fontSize(14)
        .fontColor(Color.White)
        .stateStyles({
          pressed: this.pressedStyles,
        })
        .margin({ bottom: 20 })
        .textAlign(TextAlign.Center)

    }
    .width(350).height(300).margin(10)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-20 19:46:53


相关问题
组件如何处理子组件点击事件
3671浏览 • 1回复 待解决
XComponent组件如何设置背景颜色
3021浏览 • 1回复 待解决
如何设置组件随子组件宽度变化
3062浏览 • 1回复 待解决
组件如何设置模糊效果
2581浏览 • 1回复 待解决
HarmonyOS Select组件背景如何设置
623浏览 • 1回复 待解决
HarmonyOS 组件/容器设置背景样式
1280浏览 • 1回复 待解决
如何设置组件透明效果
2853浏览 • 1回复 待解决