2015-02-08 63 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