以编程方式为按钮问题设置选择器

set Selector for Button Programmatically issues

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

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

这与所有其他主题相似。我为所有主题设置了单独的 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>

我有一个 class returns 可绘制对象(选择器)取决于所选主题。我得到的选择器如下:

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>

这行中的所有按钮都是一样的。

可绘制对象在加载时设置得很好。请参考下图。

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

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

老兄,你必须从 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" 是您必须设置选择器的地方。

希望我有所帮助。

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

如果您不想在 common state各种情况:

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

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