CheckingButton后台资源Id android

CheckingButton后台资源Id android,android,android-button,Android,Android Button,我有一个钮扣 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).setOnCli

我有一个钮扣

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(新的OnClickListener(){

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

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

你可以这样做。。 1.为按钮设置的任何背景也将此背景设置为按钮的Id。表示
button.setId(R.drawable.image);

2.然后检查状况

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

希望它能帮助您…

这似乎不可能。ressource已解析为可绘制,这就是标准功能中可以返回的所有内容。也许有一种方法可以用另一种方式将可绘制解析回id,但buttonclass中未实现此功能

如果您需要轻松访问该资源id,并且正在通过代码设置ressource,您可以编写实现Android按钮的ButtonClass,并重载/创建setBackgroundResource以将id保存在您可以访问的其他字段中。 当然,如果按钮得到了他的背景资源,而不是由于您方的函数调用,那么这是行不通的

这里有一些代码

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

我使用hashmap解决了这个问题。在花费大量时间搜索如何获取背景资源后。当我想设置背景资源,但又不想丢失设置的内容时,我做了如下操作:

HashMap<Button,Integer> hashMap;
myButton.setBackgroundResources(R.drawable.my_image);
hashMap.put(myButton,R.drawable.my_image);
记住:如果您使用现有密钥放置了另一个资源,则该值将被覆盖, 不要忘记在使用hashmap之前初始化它
这就是我希望它能帮助你的原因。

如果你要为一个按钮设置多个可绘制的背景,那么你可以按如下方式检查它

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

您可以使用
按钮。getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.name1.getConstantState())
如果上述代码不起作用,

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

这不是它的工作原理。A/你在比较不同的实例,所以aImg!=bImg,不管发生什么。B/知道你的按钮是开还是关是一个数据属性,而不是你从显示的UI中提取的东西。你应该知道你的按钮在哪个状态。有没有办法检查某个特定的按钮是否被按下?这个按钮是您可以保留对按钮的引用。如果按钮在列表中,您可以保留选中项目的列表。如果它实际上在listview中,listview会为您处理该问题,请参阅文档以了解详细信息。我在哪里可以找到此文档如此错误。您正在将按钮的id与可绘制标识符的值进行比较?永远不会平等,或者至少在任何情况下都不会平等。我可以更改按钮的背景。@njzk2这是一个非常糟糕的做法。可绘制和id是不同的东西,不应该混淆。您可以声明自己的id(不在布局中添加@+id)使用res/values/ids.xml文件,并使用它们,但不要使用ID的可绘制值。它们表示不同的内容我认为问题可能出在“==”上,请尝试“equals”。此外,我认为v.getResources()这次不起作用,因为它返回与视图相关的资源,而不是上下文。try context.getResources()相反,你能给我提供一个示例吗?我猜,你是在一个活动(例如MainActivity)中完成所有这一切的,所以使用MainActivity.this.getResources.getDrawable(R.drawable.boutton_off);if语句外壳类似于if(aImg.equals(bImg))getResources().getDrawable(R.drawable.example)在最新sdk中已弃用。请改用此ContextCompat.getDrawable(mContext,R.drawable.example))@Dasser Basyouni能否提供更多信息,如操作系统版本、实施方式等。我会留意一下。在我的4.4 KK和7.1 N ROM上进行了测试。对我来说效果很好。我已经在5.1.1华为7D-501 Beta B110 ROM上进行了测试,我已经实现了与您的第一条评论完全相同的功能,为了更确定我的结果,我将其设置为Logcat,并显示出来ys 2相同可绘制图形的不同输出:android.graphics.drawable.BitmapDrawable$BitmapState@1011644c和android.graphics.drawable.RippleDrawable$RippleState@2bf0c95Okay我将使用从JB4.1到N7.1.2的不同版本的Android操作系统进行检查和回溯测试。上述方法无法在所有LP5.x版本中工作。其他版本工作正常。试图找到一个解决办法。。。。。
    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
}
    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
}