如何以编程方式检查imageview的图像资源?

时间:2015-04-24 21:23:40

标签: java android drawable

我想查看我的imageview的背景,并确定它是否等于我的drawable文件夹中的某些drawable。我怎样才能做到这一点?我试过这段代码但没有答案:

layoutRoot.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
        if (view.getBackground() == R.drawable.grid_single_bg_selected) {
            //Do sth
        }
    }
});

2 个答案:

答案 0 :(得分:3)

When you set the background drawable also do set a tag to the imageview

imageView.setTag(R.drawable.demo);

And then compare

if (((Integer) imageView.getTag()) == R.drawable.demo){
   // Do stg
}

答案 1 :(得分:1)

If you are setting the imageView programmatically, while setting, also set a tag to this imageView. Now, when you want to find out the drawable, you can do it by retrieving the tag of this imageView.