【发布时间】:2021-04-15 12:51:36
【问题描述】:
我想更改 ontouchListener 中的背景颜色,我可以更改 textColor 但背景颜色保持不变:这是我的代码:
Button usd=findViewById(R.id.button_divide);
Button cdf=findViewById(R.id.button_multiply);
usd.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
SharedPreferences.Editor editor=sharedPref.edit();
editor.putString("currency","USD");
editor.apply();
usd.setBackgroundColor(Color.parseColor("#515de1"));
usd.setTextColor(Color.WHITE);
cdf.setBackgroundColor(Color.WHITE);
cdf.setTextColor(Color.parseColor("#515de1"));
return false;
}
});
【问题讨论】:
-
你试过 setBackgroundTint() 吗? , 另外我建议你使用 setOnClickListener 代替,如果触摸不是一个用例?
-
我用 ontouch 和 onclick 监听器尝试了 setBackgroundTint() 但我收到了来自 android studio 的消息:无法解析按钮 @oguzhanarslan 中的 setbackgroundTint
-
很抱歉,您可以在这里找到如何以编程方式将色调应用于按钮stackoverflow.com/questions/29801031/…