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

标签 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>

我有一个根据所选主题返回可绘制对象(选择器)的类。我得到的选择器如下:

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

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

最佳答案

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

您需要调用mutate()如果您不想在各种实例之间共享公共(public)状态,则在将其分配给 View 之前在您的 drawable 上:

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

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

关于android - 以编程方式为按钮问题设置选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28391339/

相关文章:

安卓选择器。按下后如何进入正常状态?

android - 自定义 View (按钮)背景选择器不起作用

android - 如何显式地在另一个模块中启动一个 Activity

java - 警报对话框不显示任何内容

java - 如何查找 xml 字符串值并将其设置为等于 java 变量?

java - 在android中创建xml文档

python - Odoo 版本 8 中带有 qweb 模板的报告

android - 如何使用 FFmpeg 平行旋转两个叠加层

android - 以编程方式将新的 Android 联系人插入手机

android - 父布局角不可见,因为选择器重叠