6

我有一个按钮

Button button = (Button)findViewById(R.id.button);
button.setBakgroundResource(R.drawable.icon);

如果我想检查哪个背景资源按钮有,可以这样做吗?如何。

例如 :

if (button.getResourceId()==R.drawable.icon)

做一点事...

更新:条件是错误的,我希望它是真实的,图像不匹配

vi.findViewById(R.id.button1).setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            v.findViewById(R.id.button1).setBackgroundResource(R.drawable.boutton_off);

            Drawable aImg = (Drawable)v.findViewById(R.id.button1).getBackground();
            Drawable bImg = v.getResources().getDrawable(R.drawable.boutton_off);

            if(aImg==bImg){

                System.out.println("On");

            }else 
                System.out.println("off");



            //main.popMessage(position);


        }

    }); 
4

8 回答 8

9

如果您为按钮设置多个可绘制背景,则按如下方式检查它

    if(button.getBackground().getConstantState()==getResources().getDrawable(R.drawable.name1).getConstantState())
{
//Your code
}
else if(button.getBackground().getConstantState()==getResources().getDrawable(R.drawable.name2).getConstantState())
{
//Your code
}
else
{
//Your code
}

button.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.name1).getConstantState())如果上面的代码不起作用,您可以使用

于 2016-10-01T15:17:13.057 回答
4

这似乎是不可能的。资源被解析为 Drawable ,这就是您可以返回标准功能的所有内容。也许有一种方法可以以另一种方式将可绘制对象解析回 id,但是此功能并未在按钮类中实现。

如果您需要轻松访问该 resourceId 并且您正在从代码设置资源源,您可以编写实现 Android Button 的 ButtonClass 并重载/创建 setBackgroundResource 以将 id 保存在您可以访问的其他字段中。当然,如果按钮获得他的 BackgroundRessource 不是由于您这边的函数调用,这当然是行不通的。

这里有一些代码。

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;

public class MyButton extends Button {
    private int bgId;

    public MyButton(Context context) {
        super(context);
    }
    public MyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void setBackgroundResource(int resId) {
        super.setBackgroundResource(resId);
        bgId = resId;   
    }

    public int getBackgroundId() {
        return bgId;
    }
}
于 2013-04-24T12:17:31.233 回答
1

我通过使用 hashmap 解决了这个问题。经过大量浪费时间搜索如何获取后台资源。当我想设置后台资源但又不想丢失我设置的内容时,我做了这样的事情:

HashMap<Button,Integer> hashMap;
myButton.setBackgroundResources(R.drawable.my_image);
hashMap.put(myButton,R.drawable.my_image);

现在,如果您想将该图像与另一张图像进行比较:

if(hashMap.get(myButton)==R.drawable.my_image)
{
 myButton.setBackgroundResources(R.drawable.another_image);
 hashMap.put(myButton,R.drawable.another_image);
}

请记住:如果您使用现有键放置了另一个资源,则该值将被覆盖不要忘记在使用之前初始化hashmap, 希望这对您有所帮助。

于 2015-07-26T01:24:07.327 回答
0

我认为 getBackground() 是您正在寻找的。它返回一个drawable,所以你可以像这样检查它:

Button myButton = ...;
Drawable myDrawable = getResources().getDrawable(R.drawable. ... ); //the one you are looking for
if(myButton.getBackground().equals(myDrawable)){
....
}
于 2013-04-24T12:14:42.850 回答
0
ImageView imageView = (ImageView) v.getTag();

if (hashMap.get(imageView) == R.drawable.hover) {
    imageView.setBackgroundResource(R.drawable.hover1);
    imageView.setTag(imageView);
    hashMap.put(imageView, R.drawable.hover1);
} else {
    imageView.setBackgroundResource(R.drawable.hover);
    imageView.setTag(imageView);
    hashMap.put(imageView, R.drawable.hover);
}
imageView.setScaleType(ScaleType.FIT_CENTER);
于 2015-08-21T06:13:53.270 回答
0

正如@Nihal 回答的那样很棒,但是 getResources() 现在已经被贬低了,在这种情况下你可以直接去 getDrawable()

对于 JAVA:

    if(button.getBackground().getConstantState()==getDrawable(R.drawable.name1).getConstantState())
{
//Your code
}
else if(button.getBackground().getConstantState()==getDrawable(R.drawable.name2).getConstantState())
{
//Your code
}
else
{
//Your code
}

对于科特林

   if(button.background.constantState==getDrawable(R.drawable.name1)?.getConstantState){
//Your code
}
else if(button.background.constantState==getDrawable(R.drawable.name2)?.getConstantState){
//Your code
}
else
{
//Your code
}
于 2021-01-12T00:28:13.043 回答
-1
Drawable aImg = (Drawable)done.getBackground();
Drawable bImg = getResources().getDrawable(R.drawable.ic_launcher);
if(aImg==bImg){

}
于 2013-04-24T12:22:41.827 回答
-1

你可以做这样的事情.. 1.无论你为你的按钮设置什么背景,也将此背景设置为按钮的 ID。意味着button.setId(R.drawable.image);

2.然后检查条件..

int temp = button.getId();
    if(temp == R.drawable.image){
    //Do something...
    }

希望对你有帮助...

于 2013-04-24T12:16:18.577 回答