1

yesterday i tried to solve the problem that my buttons don't give any visible feedback after disabling and enabling them. my problem

But the answers didn't fit in my wish how this buttons shoud be. In the end it came up that i shoud make my own buttons with drawables instead of using the stock buttons. But i disided that i don't want to change my buttons. So i hope i will finde a other way with your help.

The importaned thing to know is that i changed the stock buttons in the way it is discriped her:How to create standard Borderless buttons

my buttons in xml:

        <Button
            android:id="@+id/buttonEat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:paddingBottom="@dimen/padding_size"
            android:paddingTop="@dimen/padding_size"
            android:text="@string/button_eat"
            android:textColor="@color/white"
            android:textColorHint="@color/white"
            android:textSize="@dimen/text_size" />

I'm working with API 14 so the API 11 isue isn't the problem.

I have two methods witch are disabling and enabling my buttons. After enabling them, they still funktion but give no touch feedback.

private void startSleeping()
{
    editorState.putBoolean("SLEEPING", true);
    editorState.commit();

    buttonDrink.setEnabled(false);
    buttonEat.setEnabled(false);
    buttonWash.setEnabled(false);
    buttonDrink.setBackgroundColor(getResources().getColor(R.color.darkgray));
    buttonEat.setBackgroundColor(getResources().getColor(R.color.darkgray));
    buttonWash.setBackgroundColor(getResources().getColor(R.color.darkgray));
    buttonSleep.setBackgroundColor(getResources().getColor(R.color.orange));
    buttonWash.setTextColor(getResources().getColor(R.color.lightgray));
    buttonDrink.setTextColor(getResources().getColor(R.color.lightgray));
    buttonEat.setTextColor(getResources().getColor(R.color.lightgray));
    buttonSleep.setTextColor(getResources().getColor(color.black));
}

private void stopSleeping()
{
    editorState.putBoolean("SLEEPING", false);
    editorState.commit();

    buttonDrink.setEnabled(true);
    buttonEat.setEnabled(true);
    buttonWash.setEnabled(true);

// **Her is the problem**
// **if tried diffrent things**

// **First try: (brings back the old color but not the feedback)**
//      buttonDrink.setBackgroundColor(android.R.attr.selectableItemBackground);
// **Second try: (App crashes. Why? i don't know. The discription of selectableItemBackground says it is a drawable...)**
//buttonDrink.setBackgroundDrawable(getResources().getDrawable(android.R.attr.selectableItemBackground));
// **Third try: (eclips isn't accepting this because the attribut is a int and not a drawable)**
//buttonDrink.setBackgroundDrawable(android.R.attr.selectableItemBackground);
//**Fourth try: (brings back a feedback but changes the lock, feedback color...)**
TypedArray a = getBaseContext().obtainStyledAttributes(new int[]{android.R.attr.selectableItemBackground});
    Drawable backdraw = a.getDrawable(0);
    buttonDrink.setBackgroundDrawable(backdraw);

    buttonEat.setBackgroundColor(android.R.attr.selectableItemBackground);
    buttonWash.setBackgroundColor(android.R.attr.selectableItemBackground);
    buttonSleep.setBackgroundColor(android.R.attr.selectableItemBackground);
    buttonWash.setTextColor(getResources().getColor(R.color.white));
    buttonDrink.setTextColor(getResources().getColor(R.color.white));
    buttonEat.setTextColor(getResources().getColor(R.color.white));
    buttonSleep.setTextColor(getResources().getColor(R.color.white));
}

But there must be a way to give these buttons back funktionality from selectableItemBackground.

The problem seems to be the chaning of the background color. Do anyone have any idea? pls let me know

4

1 回答 1

1

无需更改代码中按钮的背景颜色,您可以创建自定义可绘制对象并为各种状态(启用、按下等)定义颜色。要创建自定义可绘制对象,只需在 /res/drawable 文件夹中创建一个 XML 文件,以下是一些示例内容:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" >
        <shape>
            <!-- define style for disabled state here -->
        </shape>
    </item>
    <item android:state_pressed="true" >
        <shape>
            <!-- define style for pressed state here -->
        </shape>
    </item>
    <item>
        <shape>
            <!-- define style for normal state here -->
        </shape>
    </item>
</selector>

形状标签的示例内容:

<solid
    android:color="#ffffff"/>
<stroke
    android:width="1dp"
    android:color="#000000" />
<corners
    android:radius="4dp" />
<padding
    android:left="10dp"
    android:top="10dp"
    android:right="10dp"
    android:bottom="10dp" />

现在您可以在布局 xml 文件中将此可绘制对象设置为按钮的背景。然后,您只需更改按钮的文本颜色。

于 2013-05-26T14:05:04.317 回答