我试图获取Button的背景图片并在if statement
中使用它。并检查{1000}每1000毫升if statement
。这是我的代码:
final Button btn_1 = (Button)findViewById(R.id.button1);
final Button btn_2 = (Button)findViewById(R.id.button2);
final Button btn_3 = (Button)findViewById(R.id.button3);
final ImageView a = (ImageView)findViewById(R.id.img1);
final ImageView b = (ImageView)findViewById(R.id.img2);
final ImageView c = (ImageView)findViewById(R.id.img3);
class CheckCondition extends TimerTask {
public void run() {
if (btn_1.getResources().equals(R.drawable.ic_luncher) &&
btn_2.getResources().equals(R.drawable.ic_luncher) &&
btn_3.getResources().equals(R.drawable.ic_luncher)){
a.setVisibility(View.VISIBLE);
b.setVisibility(View.VISIBLE);
c.setVisibility(View.VISIBLE);
}
else
{
a.setVisibility(View.INVISIBLE);
b.setVisibility(View.INVISIBLE);
c.setVisibility(View.INVISIBLE);
}
}
}
Timer timer = new Timer();
timer.schedule(new CheckCondition(), 0, 1000);
但它只是不起作用。即使imageviews
为真,if statement
也始终不可见。
我做错了什么?
答案 0 :(得分:1)
它总是返回false,因为你正在比较错误的返回类型“Resource”(getResource)和“int”(drawable)。
Resources resources = btn_1.getResources();
int drawable = R.drawable.ic_luncher;
如果您正在寻找类似的可绘制比较而不是getConstantState可以帮助您进行验证。
答案 1 :(得分:0)
或者您可以使用标签。
例如
button1.setTag("R.drawable.ic_luncher");
button2.setTag("R.drawable.ic_luncher");
button3.setTag("R.drawable.ic_luncher");
if (button1.getTag() == "R.drawable.ic_luncher" && button2.getTag() == "R.drawable.ic_luncher" && button3.getTag() == "R.drawable.ic_luncher") {
a.setVisibility(View.VISIBLE);
b.setVisibility(View.VISIBLE);
c.setVisibility(View.VISIBLE);
}