如何检查背景图片 [英] How to check background image

查看:142
本文介绍了如何检查背景图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有几个按钮可以有不同的背景图像。
现在,在 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

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
        }
    }
});

这篇关于如何检查背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆