问题描述
我的应用程序中有几个按钮可以有不同的背景图像。
现在,在 OnClick
函数中,我需要检查背景图像是否是drawable中的图像名称williboese。
I have a few buttons in my application that can have different background images.Now, at the OnClick
function, I need to check if the Background Image is the image name "williboese" in drawable.
我试过这样:
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
//b.setBackgroundColor(R.color.redwue);
count++;
if(arg0.getResources().equals(R.drawable.williboese)){
Toast.makeText(MainActivity.this, "heeeee", Toast.LENGTH_SHORT).show();
}
}
});
此时我知道 b
已经此背景图片但Toast未显示。我做错了什么?
I know at this moment that b
has this background image but the Toast isn´t shown. What do I make wrong?
推荐答案
在这里你要比较资源
使用 int
,它永远不会 true
...
Here you are trying to compare Resources
with int
which will be never true
...
并且这个技巧可以帮助你..
and this trick may help you..
设置后台资源ID时,将该ID设置为 View $的标记c $ c>。
When setting the background resource id, set the that ID as tag for the View
.
b.setBackgroundResource(R.drawable.williboese);
b.setTag(R.drawable.williboese);
和 OnClickListener
你可以检查哪个是当前背景drawable ...
and in OnClickListener
you can check which is the current background drawable...
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int resId = (Integer) view.getTag();
if(resId == R.drawable.williboese) {
// background is R.drawable.williboese image
}
}
});
这篇关于如何检查背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!