Button等控件设置点击效果

Button等控件设置点击效果

HarmonyOS
2024-06-05 21:12:59
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
沉默如海

@Styles自定义组件样式目前只支持CommonAttribute下的通用属性( declare class CommonAttribute extends CommonMethod<CommonAttribute> ),Button下的fontColor属性属于ButtonAttribute下的属性(declare class ButtonAttribute extends CommonMethod<ButtonAttribute> ),所以在@Styles中自定义样式中使用fontColor时IDE会报错“Property ‘fontColor’ does not exist on type ‘CommonAttribute’. <ArkTSCheck>”,可通过状态变量来实现Button不同状态下的ButtonAttribute属性修改。

示例代码

@Entry 
@Component 
struct TextExample6 { 
  // 通过状态变量来触发Button非通用属性刷新 
  @State bFontColor: string = '#000000' // Button字体颜色状态变量 
  @State bFontSize: string = '15' // Button字体大小状态变量 
 
  build() { 
    Button('我的按钮') 
      .fontColor(this.bFontColor) 
      .fontSize(this.bFontSize) 
      .stateStyles({ 
        normal: this.normalStyles, 
        pressed: this.pressedStyles, 
        disabled: this.disabledStyles, 
      }) 
      .onTouch((event?: TouchEvent) => { 
        // 触摸逻辑,通过判touchEvent的type修改状态变量触发Button刷新样式 
        if (event.type == TouchType.Up) { 
          // 按下 
          this.bFontColor = '#000000' 
          this.bFontSize = '15' 
        } else if (event.type == TouchType.Move) { 
          // 移动 
          this.bFontColor = '#FFFF00' 
          this.bFontSize = '40' 
        } 
        else { 
          // 松开/取消 
          this.bFontColor = '#00FFFF' 
          this.bFontSize = '20' 
        } 
      }) 
  } 
}
  • 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.
分享
微博
QQ
微信
回复
2024-06-06 21:31:19
相关问题
如何去掉Toggle一类控件点击效果
1009浏览 • 1回复 待解决
如何禁止Button点击事件?
1320浏览 • 1回复 待解决
如何取消Button默认的按压效果?
1426浏览 • 1回复 待解决
HarmonyOS 控件点击事件无响应
814浏览 • 1回复 待解决
「多态控件」的效果样式确认
2206浏览 • 1回复 待解决
HarmonyOS如何增加控件点击热区?
1255浏览 • 1回复 待解决
打开授权设置Button的实现
1058浏览 • 1回复 待解决
HarmonyOS 如何实现抽屉效果控件
548浏览 • 1回复 待解决
Button组件如何设置渐变背景色
3767浏览 • 1回复 待解决
点击事件,@State 页面未生效,在线
3675浏览 • 0回复 待解决
HarmonyOS 如何设置控件样式?
851浏览 • 1回复 待解决
如何实现按钮的点击效果
1272浏览 • 2回复 待解决
HarmonyOS 如何实现图中的input控件效果
1033浏览 • 1回复 待解决