ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

android – 为按钮设置选择器以编程方式发布

2019-07-11 19:24:16  阅读:195  来源: 互联网

标签: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(选择器).我得到选择器如下:

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>

这对于行中的所有按钮都是相同的.

可绘制的负载设置得很好.请参考下图.

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

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

解决方法:

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

如果您不希望在各种实例中共享公共状态,则需要在绘制之前调用mutate(),然后才能将其分配给View:

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

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

标签:android,xml,android-selector
来源: https://codeday.me/bug/20190711/1435258.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有