检查按钮背景资源id android

7
我有一个按钮。
Button button = (Button)findViewById(R.id.button);
button.setBakgroundResource(R.drawable.icon);

如果我想检查某个按钮使用了哪个背景资源,有可能吗?如何实现。

例如:

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

使用代码做一些事情...

更新:条件为false,我希望它变成true,图像不匹配

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

这不是它的工作方式。A/你正在比较不同的实例,所以无论如何aImg != bImg。B/知道你的按钮是开还是关是一个数据属性,而不是从用户界面中获取的。这是显示(Display)而不是状态(State),你应该知道你的按钮处于哪种状态。 - njzk2
有没有办法检查特定的按钮是否被按下?这个按钮在列表中并且具有相同的ID。所以忘记ID。 - Christina
你可以保留按钮的引用。如果它在列表中,你可以保留选中项的列表。如果它实际上在ListView中,ListView会为你处理,详见文档。 - njzk2
我在哪里可以找到这份文档? - Christina
http://developer.android.com - njzk2
8个回答

10
如果您要为按钮设置多个可绘制背景,则可以按照以下方式进行检查。
    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()),如果上面的代码不起作用。

1
在最新的SDK中,getResources().getDrawable(R.drawable.example)已经被弃用。请使用**ContextCompat.getDrawable(mContext, R.drawable.example))**。 - Nihal
好的,我会检查并回复。 - Nihal
同时尝试使用已弃用的方法并进行检查。我之前使用过那个方法。 - Nihal
我之前也尝试过同样的事情,使用了“==”和“.equal()”,但我不知道出了什么问题。 - Dasser Basyouni
1
测试了不同版本的Android操作系统,从JB 4.1到N 7.1.2。以上方法在所有LP 5.x版本中均无法工作。其他版本正常运行。正在尝试寻找解决方法... - Nihal
显示剩余2条评论

5
似乎不可能。该资源被解析为Drawable,这就是您在标准功能中可以获取的全部内容。也许有一种方式可以以另一种方式将drawable解析回id,但是此功能未在buttonclass中实现。
如果您需要轻松访问该resourceId,并且从代码设置了resource,那么您可以编写实现Android Button并重载/创建setBackgroundResource的ButtonClass来保存附加字段中的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;
    }
}

1
我使用哈希表解决了这个问题。在浪费了很多时间搜索如何获取背景资源之后,当我想要设置背景资源但又不想失去我所设置的内容时,我做了类似于以下的事情:
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);
}

记住:如果您使用已存在的键放入另一个资源,则该值将被覆盖在使用哈希映射之前不要忘记初始化它 以上就是全部内容,希望对你有所帮助。


1

正如 @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
}

对于 Kotlin

   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
}

0

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

Button myButton = ...;
Drawable myDrawable = getResources().getDrawable(R.drawable. ... ); //the one you are looking for
if(myButton.getBackground().equals(myDrawable)){
....
}

我认为问题可能出在"=="上,尝试使用"equals"代替。另外,我认为v.getResources()这次不会起作用,因为它返回与视图相关的资源,而不是上下文。请尝试使用context.getResources()。 - Analizer
你能给我提供一个样例吗? - Christina
我猜你是在一个活动中完成这些操作(例如MainActivity),所以使用MainActivity.this.getResources.getDrawable(R.drawable.boutton_off);,if语句应该像if(aImg.equals(bImg))这样。 - Analizer

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

-1
Drawable aImg = (Drawable)done.getBackground();
Drawable bImg = getResources().getDrawable(R.drawable.ic_launcher);
if(aImg==bImg){

}

-1
你可以像这样做... 1.无论你为按钮设置什么背景,都将此背景设置为按钮的ID。意思是button.setId(R.drawable.image); 2.然后检查条件...
int temp = button.getId();
    if(temp == R.drawable.image){
    //Do something...
    }

希望它能对你有所帮助...


非常错误。你正在比较一个按钮的id和一个可绘制标识符的值?永远不会相等,或者至少在任何你想要的情况下都不会相等。 - njzk2
1
对我来说,更改按钮背景很好用。@njzk2 - AndiM
这是一种非常糟糕的做法。Drawables和IDs是不同的东西,不应该混淆使用。您可以在res/values/ids.xml文件中声明自己的IDs(而无需在布局中添加@+id),并使用它们,但不要将Drawable值用作IDs。它们代表不同的事物。 - njzk2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接