按钮设置选择器以编程方式发出

时间:2015-02-08 06:42:51

标签: android xml android-selector

我有一排按钮,我正在以编程方式为背景和文本设置选择器。我想以编程方式执行此操作的原因是因为,我有一组用户可以选择的主题,并且根据所选主题,我想更改按钮的选择器。

例如,如果用户选择蓝色主题,则在加载时,按钮的背景为蓝色,文本颜色为白色。当他按下按钮时,背景变为白色,文本颜色变为蓝色。当用户从按钮上移开手指时,更改将恢复为默认蓝色表示背景,白色表示文本颜色。您可以在下面看到相应的蓝色选择器。

这与所有其他主题类似。我为所有主题都有单独的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>

我有一个类,它根据所选主题返回drawable(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" />

Total Row的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>

对于行中的所有按钮,这是相同的。

drawable在负载上设置得很好。请参考下图。

enter image description here

问题是当我点击一个按钮(例如A)时,相邻的ImageButton(麦克风)也会改变其状态。请看下面的图片:

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

为什么会这样?有人可以帮我弄这个吗。如果您需要任何其他信息,请与我们联系。

2 个答案:

答案 0 :(得分:25)

我认为您遇到了 mutate 相关问题(请查看here,它非常有用)

如果您不希望在各种实例中共享公共状态,则需要在您的drawable上致电mutate(),然后再与View联系:

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

在您的代码中,您使用相同的Drawable进行多次View,因此您需要采用我上面描述的方法来避免状态共享。

答案 1 :(得分:1)

Dude你必须设置选择器来自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"是您必须设置选择器的地方。

希望我帮助过。

相关问题