HarmonyOS Button stateEffect自定义咨询

开启Button stateEffect为true后,如何自定义按压后的背景颜色或者按压后的背景图标?

HarmonyOS
2025-01-09 16:28:18
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
aquaa

可以使用在Button组件上定制Button内容区的方法,自定义class来实现ContentModifier接口。参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-button-V5#contentmodifier12

class MyButtonStyle implements ContentModifier<ButtonConfiguration> {
  x: number = 0
  y: number = 0
  selectedColor: Color = Color.Black

  constructor(x: number, y: number, ColorType: Color) {
    this.x = x
    this.y = y
    this.selectedColor = ColorType
  }

  applyContent(): WrappedBuilder<[ButtonConfiguration]> {
    return wrapBuilder(buildButton1)
  }
}

@Builder
function buildButton1(config: ButtonConfiguration) {
  Column({ space: 30 }) {
    Text(config.enabled ? "enabled true" : "enabled false")
    Text('圆圈状态' + (config.pressed ? "( 按压 )" : "( 非按压 )"))
    Text('点击位置x坐标:' + (config.enabled ? (config.contentModifier as MyButtonStyle).x : "0"))
    Text('点击位置y坐标:' + (config.enabled ? (config.contentModifier as MyButtonStyle).y : "0"))
    Circle({ width: 50, height: 50 })
      .fill(config.pressed ? (config.contentModifier as MyButtonStyle).selectedColor : Color.Black)
      .gesture(
        TapGesture({ count: 1 }).onAction((event: GestureEvent) => {
          config.triggerClick(event.fingerList[0].localX, event.fingerList[0].localY)
        })).opacity(config.enabled ? 1 : 0.1)
  }
}

@Entry
@Component
struct ButtonExample {
  @State buttonEnabled: boolean = true;
  @State positionX: number = 0
  @State positionY: number = 0
  @State state: boolean[] = [true, false]
  @State index: number = 0

  build() {
    Column() {
      Button('OK')
        .contentModifier(new MyButtonStyle(this.positionX, this.positionY, Color.Red))
        .onClick((event) => {
          console.info('change' + JSON.stringify(event))
          this.positionX = event.displayX
          this.positionY = event.displayY
        }).enabled(this.buttonEnabled)
      Row() {
        Toggle({ type: ToggleType.Switch, isOn: true }).onChange((value: boolean) => {
          if (value) {
            this.buttonEnabled = true
          } else {
            this.buttonEnabled = false
          }
        }).margin({ left: -80 })
      }
    }.height('100%').width('100%').justifyContent(FlexAlign.Center)
  }
}
  • 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.
分享
微博
QQ
微信
回复
2025-01-09 19:27:13
相关问题
HarmonyOS 自定义相册方案咨询
768浏览 • 1回复 待解决
HarmonyOS 自定义扫码咨询
630浏览 • 1回复 待解决
HarmonyOS自定义相册选择页面咨询
1065浏览 • 1回复 待解决
HarmonyOS 工具类自定义弹窗方案咨询
835浏览 • 1回复 待解决
多module场景Hvigor自定义扩展咨询
1470浏览 • 1回复 待解决
自定义弹窗自定义转场动画
1955浏览 • 1回复 待解决
HarmonyOS 定义自定义组件
1046浏览 • 1回复 待解决
HarmonyOS 自定义UA
715浏览 • 1回复 待解决
HarmonyOS 自定义键盘
1025浏览 • 1回复 待解决
HarmonyOS 自定义Slider
895浏览 • 1回复 待解决
HarmonyOS 自定义Dialog宽度
854浏览 • 1回复 待解决
HarmonyOS 自定义拍照功能
806浏览 • 1回复 待解决
HarmonyOS 如何自定义tab
1455浏览 • 2回复 待解决