首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在更改Backgroundcolor后从无边框按钮获得反馈?selectableItemBackground

如何在更改Backgroundcolor后从无边框按钮获得反馈?selectableItemBackground
EN

Stack Overflow用户
提问于 2013-05-26 21:24:17
回答 1查看 640关注 0票数 1

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

但答案并不符合我的愿望,这些按钮应该是什么样子的。最后,我想出了一个办法,那就是我应该用抽屉做我自己的纽扣,而不是用库存的纽扣。但我犹豫了一下,我不想改变我的按钮。所以我希望在你的帮助下我能找到另一条路。

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

xml中的my buttons:

代码语言:javascript
运行
复制
        <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" />

我有两种方法,分别是禁用和启用我的按钮。启用它们后,它们仍然工作,但不提供触摸反馈。

代码语言:javascript
运行
复制
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中恢复功能。

有谁知道吗?请让我知道

EN

Stack Overflow用户

回答已采纳

发布于 2013-05-26 22:05:04

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

代码语言:javascript
运行
复制
<?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>

形状标记的示例内容:

代码语言:javascript
运行
复制
<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文件中将此可绘制设置为按钮的背景。然后,您只需更改按钮的文本颜色。

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16759665

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档