java - 更改背景颜色后如何从无边框按钮中获取反馈?可选项目背景

标签 java android button borderless

昨天我试图解决我的按钮在禁用和启用它们后没有提供任何可见反馈的问题。 my problem

但是答案并不符合我希望这个按钮应该是什么样子。最后我想到我应该用可绘制对象制作我自己的按钮而不是使用库存按钮。 但我认为我不想更改我的按钮。 所以我希望我能在您的帮助下找到其他方法。

要知道的重要一点是,我按照她描述的方式更改了库存按钮:How to create standard Borderless buttons

我在 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" />

我正在使用 API 14,所以 API 11 问题不是问题。

我有两种方法可以禁用和启用我的按钮。 启用它们后,它们仍然起作用但没有提供触摸反馈。

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

但是必须有一种方法可以让这些按钮从 selectableItemBackground 恢复功能。

问题似乎是背景颜色的变化。 有人知道吗?请告诉我

最佳答案

无需在代码中更改按钮的背景颜色,您可以创建自定义可绘制对象并在其中定义各种状态(启用、按下等)的颜色。 要创建自定义可绘制对象,只需在/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 文件中将此可绘制对象设置为按钮的背景。然后您只需更改按钮的文本颜色。

关于java - 更改背景颜色后如何从无边框按钮中获取反馈?可选项目背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759665/

相关文章:

android - glSurfaceView 和 camera onResume 问题

android - 自定义 ListView, getView 方法

android - 屏幕关闭时检查音量按钮的使用情况

javascript - Phonegap 更改按钮值 onclick

java - Rest 端点异常处理

java - 对于 java 版本 1.8,我应该使用哪个版本的 tomcat/java docker?

android - AndroidManifest.xml 文件错误!

JavaScript 和范围问题

Java EJB org.apache.jasper.JasperException : java. lang.NullPointerException

java - JSF 性能限制