如何使按钮变灰?

时间:2012-01-05 13:15:38

标签: android button

我有一个如下所示的按钮。当我想禁用它时,我使用my_btn.setEnabled(false),但我也希望将其变灰。我怎么能这样做?

由于

<Button android:id="@+id/buy_btn" style="@style/srp_button" />

风格/ srp_button

<style name="srp_button" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/btn_default</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#ffffff</item>
    <item name="android:textSize">14sp</item>
    <item name="android:typeface">serif</item>
    <item name="android:paddingLeft">30dp</item>
    <item name="android:paddingRight">30dp</item>
    <item name="android:paddingTop">5dp</item>
    <item name="android:paddingBottom">5dp</item>
</style>

抽拉/ btn_default.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/pink" />
    <corners android:radius="6dp" />
</shape>

10 个答案:

答案 0 :(得分:141)

您还可以通过设置alpha(使其半透明)使其显示为已禁用。如果您的按钮背景是图像,并且您不想为其创建状态,这将非常有用。

button.setAlpha(.5f);
button.setClickable(false);

答案 1 :(得分:51)

您必须在btn_defaut.xml中提供3个或4个州作为选择器。

  1. 按下状态
  2. 默认状态
  3. 焦点状态
  4. 启用状态(禁用带错误指示的状态;请参阅注释)
  5. 您将相应地为州提供效果和背景。

    以下是详细讨论:Standard Android Button with a different color

答案 2 :(得分:45)

最简单的解决方案是在我看到here

时将滤色器设置为按钮的背景图像

您可以执行以下操作:

if ('need to set button disable')
    button.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
else
    button.getBackground().setColorFilter(null);

希望我能帮助别人......

答案 3 :(得分:11)

将Clickable设置为false并将背景颜色更改为:

callButton.setClickable(false);
callButton.setBackgroundColor(Color.parseColor("#808080"));

答案 4 :(得分:11)

所有给定的答案都可以正常工作,但我记得我知道使用setAlpha可以提高性能bad idea(更多信息here)。因此,创建StateListDrawable是管理禁用按钮状态的更好主意。以下是:

在res / drawable文件夹中创建XML btn_blue.xml:

Optional<IGhRepo> highestContributors(Stream<IGhOrg> organizations){
    return organizations
            .flatMap(IGhOrg::getRepos)
            .max((repo1,repo2)-> (int)repo1.getContributors().count() - (int)repo2.getContributors().count() );
}

在res / values / styles.xml中创建按钮样式

<!-- Disable background -->
<item android:state_enabled="false"
      android:color="@color/md_blue_200"/>

<!-- Enabled background -->
<item android:color="@color/md_blue_500"/>

然后将此样式应用于您的按钮:

<style name="BlueButton" parent="ThemeOverlay.AppCompat">
      <item name="colorButtonNormal">@drawable/btn_blue</item>
      <item name="android:textColor">@color/md_white_1000</item>
</style>

现在,当您拨打<Button android:id="@+id/my_disabled_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/BlueButton"/> btnBlue.setEnabled(true)时,状态颜色将自动切换。

答案 5 :(得分:8)

您应该为禁用按钮创建XML文件( drawable / btn_disable.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/grey" />
    <corners android:radius="6dp" />
</shape>

为按钮创建一个选择器( drawable / btn_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_disable" android:state_enabled="false"/>
    <item android:drawable="@drawable/btn_default" android:state_enabled="true"/>
    <item android:drawable="@drawable/btn_default" android:state_pressed="false" />

</selector>

将选择器添加到按钮

<style name="srp_button" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/btn_selector</item>
</style>

答案 6 :(得分:4)

Button button = (Button)findViewById(R.id.buy_btn);
button.setEnabled(false);

答案 7 :(得分:0)

你可以制作一张灰色图片并放入你的绘图中并将其添加到你的代码中。

button.setBackgroundResource(R.drawable.grey);

简单,简单,但由于使用图片,它会获得更多空间。

答案 8 :(得分:0)

我为此使用了以下代码:

ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
profilePicture.setColorFilter(filter);

答案 9 :(得分:0)

我尝试了上述解决方案,但是似乎没有一种对我有用。我选择了以下选项:

<!-- button_color_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorAccent" android:state_enabled="true"/>
    <item android:color="@color/colorAccentLight" android:state_enabled="false"/>
</selector>
<com.google.android.material.button.MaterialButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/button_color_selector"
                .../>