题目
下面哪段代码在Button组件被按下时会改变背景色?A. ``` Button('Button2') .stateStyles(( pressed: { backgroundColor('#ff707070') ) }) .margin('30%') ```B. ``` Button('Button2') .stateStyles(( normal: { backgroundColor('#ff2787d9') ) }) .margin('30%') ```C. ``` Button('Button2') .stateStyles(( disabled: { backgroundColor('#ff707070') ) }) .margin('30%') ```
下面哪段代码在Button组件被按下时会改变背景色?
A. ``` Button('Button2') .stateStyles({ pressed: { backgroundColor('#ff707070') } }) .margin('30%') ```
B. ``` Button('Button2') .stateStyles({ normal: { backgroundColor('#ff2787d9') } }) .margin('30%') ```
C. ``` Button('Button2') .stateStyles({ disabled: { backgroundColor('#ff707070') } }) .margin('30%') ```
题目解答
答案
A. ``` Button('Button2') .stateStyles({ pressed: { backgroundColor('#ff707070') } }) .margin('30%') ```
解析
本题考查Button组件的状态样式设置,核心在于理解不同状态对应的样式触发条件。
- 关键知识点:stateStyles方法用于定义组件在不同状态下的样式,常见状态包括normal(正常)、pressed(按下)、disabled(禁用)。
- 破题关键:明确题目要求的是“按下时”改变背景色,因此需定位到对应 pressed状态的选项。
选项分析
选项A
Button('Button2') 
.stateStyles({ 
pressed: { 
backgroundColor('#ff707070') 
} 
}) 
.margin('30%') - 逻辑:通过 .stateStyles方法设置pressed状态下的backgroundColor,表示按钮被按下时背景色变为指定值。
- 结论:符合题意。
选项B
Button('Button2') 
.stateStyles({ 
normal: { 
backgroundColor('#ff2787d9') 
} 
}) 
.margin('30%') - 逻辑:设置 normal状态下的背景色,仅在按钮未被按下时生效。
- 结论:与题目要求无关。
选项C
Button('Button2') 
.stateStyles({ 
disabled: { 
backgroundColor('#ff707070') 
} 
}) 
.margin('30%') - 逻辑:设置 disabled状态下的背景色,仅在按钮被禁用时生效。
- 结论:与题目要求无关。