2013-04-24 83 views
4

我有一个按钮CheckingButton背景资源ID的Android

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


     } 

    }); 
+0

这不是它是如何工作的。 A /你正在比较不同的实例,所以aImg!= bImg无论如何。 B /知道你的按钮是打开还是关闭是一个Data属性,而不是你从用户界面(Display)拉出来的。你应该知道你的按钮在哪个状态。 – njzk2 2013-04-24 12:33:50

+0

有没有办法检查某个按钮是否被按下?此按钮位于列表中并具有相同的ID。所以忘记ID – Christina 2013-04-24 12:45:11

+0

你可以保持对按钮的引用。如果它在列表中,则可以保留已选项目的列表。如果它实际上是在listview中,listview会为你处理,请参阅文档以获取详细信息。 – njzk2 2013-04-24 12:53:39

回答

0

我觉得getBackground()就是你要找的东西。它返回一个绘制,这样你就可以检查它像这样:

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

请检查更新的问题 – Christina 2013-04-24 12:32:02

+0

我认为问题可能与“==”,请尝试“等于”代替。而且,我认为v.getResources()这次不会工作,因为它返回与视图相关的资源,而不是上下文。尝试使用context.getResources()来代替。 – Analizer 2013-04-24 12:43:08

+0

你能否给我提供一个样例请 – Christina 2013-04-24 12:47:00

-1

你可以做这样的事情..你为你的按钮设置 1.Whatever背景也设置此背景button.Means button.setId(R.drawable.image);的ID

2.然后检查条件..

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

希望它可以帮助你......

+0

请检查更新后的问题 – Christina 2013-04-24 12:30:53

+0

那么错误。你正在比较一个按钮的ID和可绘制标识符的值?永远不会平等,或者至少在任何情况下都不需要。 – njzk2 2013-04-24 12:34:58

+1

它适用于我改变按钮的背景.. @ njzk2 – AndiM 2013-04-24 13:09:41

3

这似乎不是可能的。资源被解析为Drawable,这就是所有你可以回到标准功能。也许有办法以另一种方式将drawable解析回id,但是这个功能并没有在buttonclass中实现。

如果您需要轻松访问该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; 
    } 
} 
+0

请检查更新后的问题 – Christina 2013-04-24 12:31:25

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

} 
+0

其中'done'是您的按钮名称... – 2013-04-24 12:23:31

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的活动正用于 这是所有希望这可以帮助您之前。

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

如果要设置多个绘制背景的一个按钮,然后你选择了它,如下所示

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

** getResources()。getDrawable(R.drawable.example)**在最新sdk中已弃用**。而是使用** ** ContextCompat.getDrawable(mContext,R.drawable.example)** ** – Nihal 2017-03-02 12:38:48

+0

刚刚测试过我没有工作 – 2017-03-12 18:54:23

+0

@Dasser Basyouni你能提供更多的信息,比如操作系统版本,你如何实现等等。有一个了望。在我的4.4 KK和7.1 Nroms上测试过。为我工作得很好。 – Nihal 2017-03-14 01:06:34