How to check image resource of an imageview programmatically?

风流意气都作罢 提交于 2019-12-24 05:04:10

问题


I want to check background of my imageview and do sth if it equals to some drawable in my drawable folder. How can I do that? I have try this code but no answer:

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

回答1:


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
}



回答2:


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.



来源:https://stackoverflow.com/questions/29857744/how-to-check-image-resource-of-an-imageview-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!