2015-02-08 27 views
3

我有一排按鈕,我編程設置其選擇器的背景和文本。我想這樣做的原因是因爲,我有一套主題,用戶可以選擇,並根據所選的主題,我想改變按鈕的選擇器。組選擇編程問題

例如,如果用戶選擇了藍色的主題,在加載時,所述按鈕的背景是藍色和文本顏色爲白色。當他按下按鈕時,背景變爲白色,文字顏色變爲藍色。當用戶從按鈕中移除手指時,所做的更改會恢復爲默認背景爲藍色,而白色爲文本顏色。您可以在下面看到藍色的各個選擇器。

這與所有其他主題。我爲所有主題分開了XML。文字顏色更改的選擇器正常工作。問題在於按鈕的背景選擇器。

selector_background_blue.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@android:color/white" android:state_pressed="true"/> 
    <item android:drawable="@color/blue_500"/> 

</selector> 

color_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true" android:color="@color/blue_500"/> 
    <item android:color="@android:color/white"/> 

</selector> 

我有取決於所選擇的主題,它返回被拉伸(選擇器)的類。

public Drawable getButtonBackgrounds(String theme) { 
    Drawable drawable = null; 

    if (theme.equalsIgnoreCase(Const.Theme.BLUE)) 
     drawable = context.getResources().getDrawable(
       R.drawable.selector_background_blue); 

    return drawable; 
} 

我對按鈕的背景如下設置這些選擇:我如下得到選擇

private void setButtonBackgrounds(Drawable buttonDrawable) { 
int sdk = android.os.Build.VERSION.SDK_INT; 

     if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { 
      btnA.setBackgroundDrawable(buttonDrawable); 
      btnT.setBackgroundDrawable(buttonDrawable); 
      ..... 
      ..... 
      btnVoice.setBackgroundDrawable(buttonDrawable); 
     } else { 
      btnA.setBackground(buttonDrawable); 
      btnT.setBackground(buttonDrawable); 
      ..... 
      ..... 
      btnVoice.setBackground(buttonDrawable); 
     } 
} 

按鈕的xml

<Button 
    android:id="@+id/btnT" 
    android:layout_width="0dip" 
    android:layout_height="match_parent" 
    android:layout_weight="0.20" 
    android:background="?android:attr/selectableItemBackground" 
    android:text="@string/button_t" 
    android:textSize="22sp" /> 

總計行的XML:

<LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/btnA" 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="0.20" 
       android:background="?android:attr/selectableItemBackground" 
       android:text="@string/arithmetic_symbol" 
       android:textSize="16sp" /> 

      <Button 
       android:id="@+id/btnT" 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="0.20" 
       android:background="?android:attr/selectableItemBackground" 
       android:text="@string/trigonometric_symbol" 
       android:textSize="16sp" /> 

      <Button 
       android:id="@+id/btnN" 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="0.20" 
       android:background="?android:attr/selectableItemBackground" 
       android:text="@string/voice_calculator_symbol" 
       android:textSize="16sp" 
       android:visibility="gone" /> 

      <ImageButton 
       android:id="@+id/btnVC" 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="0.20" 
       android:background="?android:attr/selectableItemBackground" 
       android:contentDescription="@string/empty" 
       android:src="@drawable/ic_keyboard_voice_black" 
       android:text="" /> 

      <Button 
       android:id="@+id/btnC" 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="0.20" 
       android:background="?android:attr/selectableItemBackground" 
       android:text="@string/button_c" 
       android:textSize="16sp" /> 

      <Button 
       android:id="@+id/btnD" 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="0.20" 
       android:background="?android:attr/selectableItemBackground" 
       android:text="@string/button_del" 
       android:textSize="16sp" /> 
     </LinearLayout> 

這對於該行中的所有按鈕都是相同的。

提拉設置就好上的負載。請參考下面的圖片。

enter image description here

的問題是當我點擊一個按鈕(用於離。,A)中,相鄰的ImageButton(麥克風)也改變其狀態。請看看下面的圖片:

enter image description here enter image description here enter image description here enter image description here

這究竟是爲什麼?有人可以幫我弄這個嗎。如果您需要其他信息,請告訴我。

+0

這些按鈕的容器是什麼類?我的意思是,它們是否包含在LinearLayout,FrameLayout或其他內容中?你有任何聽衆連接到這些按鈕? – aga 2015-02-10 07:17:04

+0

它們在線性佈局內。將在1小時內用XML更新問題 – 2015-02-10 07:19:23

+0

@aga,更新行的xml問題。每個按鈕都有一個onClickListener附加到它。 – 2015-02-10 09:23:33

回答

21

我認爲你正在經歷一個發生變異 - 相關問題(請看看here,它的極端有用)

你需要它assingning到View之前調用您的繪製mutate()如果YOUT不要」不想跨越各種實例共享常見的狀態

Drawable buttonDrawable = context.getResources().getDrawable(R.drawable.btn); 
buttonDrawable.mutate() 
btnA.setBackgroundDrawable(buttonDrawable); 

在你的代碼使用相同的Drawable更多然後一個View,所以你需要採用我上面描述的方法來避免狀態共享。

+0

將試一試,並回復給您... – 2015-02-10 12:12:14

+0

男人你救了我的命..似乎工作.. +1直到我通過測試應用程序,並獎勵你的賞金。 – 2015-02-10 12:17:11

+0

@ VamsiChalla很高興聽到這個消息。如果您有任何其他問題,請告訴我們。 – bonnyz 2015-02-10 13:35:56

1

活計,你必須設置選擇從XML文件請看下圖:

<Button 
android:id="@+id/btnT" 
android:layout_width="0dip" 
android:layout_height="match_parent" 
android:layout_weight="0.20" 
android:background="@drawable/button_selector" 
android:text="@string/button_t" 
android:textSize="22sp" /> 

這裏的物業android:background="@drawable/you_drawable_selector"是,你必須要設置的選擇。

希望我幫助。

+0

我知道,但我的選擇器根據用戶選擇的主題而變化,因此我正在以編程方式設置選擇器。 – 2015-02-08 07:15:41

+0

看看這個方法: private void setButtonBackgrounds(Drawable buttonDrawable) – 2015-02-08 07:21:51

+0

是的,這就是我想弄明白的.. – 2015-02-08 07:23:24