设置选择器按钮编程问题 [英] set Selector for Button Programmatically issues

查看:194
本文介绍了设置选择器按钮编程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一排按钮,我编程设置其选择器的背景和文本。我想以编程方式做到这一点的原因是因为,我有一组主题的用户可以从中选择,并根据所选择的主题,我想改变按钮的选择。

I have a row of buttons and i am setting their selectors for background and text programatically. The reason i want to do this programmatically is because, I have a set of themes the user can choose from and depending upon the theme selected, i want to change the selector for the button.

例如,如果用户选择了一个蓝色的主题,在加载时,所述按钮的背景是蓝色和文本颜色为白色。当他presses按钮,背景变为白色,文本颜色变为蓝色。当用户从按钮的手指,这些变化恢复到默认的蓝色背景和白色文本颜色。你可以看到各选择下面蓝色的。

For example, if the user selects a blue theme, when loaded, the background of the button is blue and text colour is white. When he presses the button, the background changes to white and the text colour changes to blue. When user removes the finger from button, the changes revert back to default blue for background and white for text colour. You can see the respective selectors for blue below.

这是类似于其他主题。我对所有的主题不同的XML的。文本颜色变化选择工作正常。问题是与背景选择器按钮。

This is similar to all other themes. I have separate XMLs for all the themes. The selector for text colour change works fine. The problem is with the background selector for button.

selector_background_blue.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

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>

我有根据选择的主题,它返回被拉伸(选择器)的一类。我得到的选择如下:

I have a class that returns the drawable(selector) depending upon the theme selected. I am getting the selector as follows:

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;
}

我如下设置这些选择器按钮的背景:

I'm setting these selector for button's background as follows:

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>

这是该行中的所有按钮是相同的。

This is the same for all the buttons in the row.

提拉设置在负载就好了。请参考下面的图片。

The drawable is set just fine on the load. Please refer to the image below.

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

The problem is When I click on a button(for ex., A), the adjacent ImageButton(microphone) is also changing its state. Please look at the images below:




这是怎么回事?有人可以帮助我。请让我知道如果你需要任何其他信息。

Why is this happening? Can someone help me with this. Please let me know if you need any other info.

推荐答案

我认为你正在经历的变异 - 相关的问题(请大家看看的这里,这是非常有用的极端)

I think that you are experiencing a mutate-related issue (please take a look here, it's extremly useful)

您需要调用<一个href=\"http://developer.android.com/reference/android/graphics/drawable/Drawable.html#mutate()\"><$c$c>mutate()您绘制它assingning之前的查看如果YOUT不想共享的常见的状态的在各种实例:

You need to call mutate() on your drawable before assingning it to the View if yout don't want to share the common state across the various instances:

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

在您的code您使用的是相同可绘制的一个以上的查看,所以你需要采取我上面介绍的方法,以避免国家分享。

In your code you are using the same Drawable for more then one View, so you need to adopt the approach I've described above to avoid the state-sharing.

这篇关于设置选择器按钮编程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆