【发布时间】:2019-01-15 11:18:49
【问题描述】:
我有一个非常简单的问题
考虑我正在使用getText() 方法在线检索字符串值
现在根据字符串的值,我将按钮背景设置为红色和蓝色。
如果字符串值为红色,则按钮背景为红色,如果为蓝色,则为蓝色。
现在,如果我将onClicklistener 实现到同一个按钮,我想更改它的背景颜色。如果是红色则将其更改为蓝色,如果是蓝色则将其更改为红色,只要用户按键即可。
mSolved = (Button) itemView.findViewById(R.id.book_solved);
mSolved.setText(g.getColorvalue());
if("Blue".equals(holder.mSolved.getText())){
mSolved.setBackgroundColor(BLUE);
}
if("Red".equals(holder.mSolved.getText())){
.mSolved.setBackgroundColor(RED);
}
mSolved.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Background color is already BLue)
{
change to Red
}
else
{
Change to Blue
}
}
【问题讨论】:
-
可以使用FLAG变量来实现逻辑
-
你能给我举个例子吗?
-
检查答案。使用类似的 FLAG 变量
标签: java android onclick android-button