【发布时间】:2016-04-30 21:14:25
【问题描述】:
我有 3 个按钮,默认情况下它们都具有相同的可绘制背景 (subject_button)
我想做的事:
当我单击一个按钮时,他的背景会更改(变为clicked_subject),所有其他按钮都保持默认背景,如果我在单击另一个按钮后单击一个按钮,我刚刚单击的按钮会更改他的背景,而前一个按钮会返回到初始背景,只允许一个按钮具有clicked_subject 背景,如果再次单击不同的按钮,他的背景会回到初始背景,让所有按钮具有初始背景。
问题:
如果我点击不同的按钮,他的背景保持不变,而不是变回原来的背景。
我的逻辑:
theButton1.setBackgroundResource(R.drawable.subject_button);
theButton1.setOnClickListener(this);
//same for other 2
@Override
public void onClick(View v) {
if (v.getBackground() == ContextCompat.getDrawable(this, R.drawable.subject_button)) {
theButton1.setBackgroundResource(R.drawable.subject_button);
theButton2.setBackgroundResource(R.drawable.subject_button);
theButton3.setBackgroundResource(R.drawable.subject_button);
v.setBackgroundResource(R.drawable.clicked_subject);
} else {
v.setBackgroundResource(R.drawable.subject_button);
}
为什么会这样?
【问题讨论】: