2013-04-24 82 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