2012-01-05 200 views
71

我有一个按钮,如下所示。当我想禁用它时,我使用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> 
+2

使用选择器http://stackoverflow.com/questions/7335345/button-background-selector – 2012-01-05 13:19:00

回答

44

你必须提供3个或4种状态在btn_defaut.xml作为一个选择。

  1. 按下状态
  2. 默认状态
  3. 对焦状态
  4. 启用状态(关闭状态错误指示;见注释)

您将提供相应的状态效果和背景。

这里是一个详细的讨论:Standard Android Button with a different color

+0

所以为了变灰,我必须改变背景的颜色和禁用状态下文本的颜色?没有办法只是添加一个透明的前景? – jul 2012-01-05 13:22:43

+3

是啊!你将为'android:state_disable =“true”提供什么,它将显示当Button被禁用时的状态,简单和推荐的方式。 – 2012-01-05 13:25:35

+0

和我可以在哪里指定禁用状态的文本颜色?似乎只有背景可以指定......我问了一个新的问题:http://stackoverflow.com/questions/8743584/how-to-set-the-text-style-of-a-button- in-selectors – jul 2012-01-05 13:49:17

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

这并没有回答这个问题。 – 2018-01-16 06:14:47

9

组可单击为假,改变backgroung颜色:

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

最简单的办法是彩色滤光片设置为背景图片我看到一个按钮here

你可以做如下:

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

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

+0

我最喜欢这种方法。谢谢! – mtb 2014-01-06 01:12:24

+1

这种方法有潜力,但最终的颜色并不总是你想象的那样。例如,具有灰色颜色的橙色按钮将结果以深红色相乘,而不是褪色的橙色。 – 2015-03-11 21:52:51

+2

也许'Mode.SRC_IN'应该用来代替乘法。请参阅http://stackoverflow.com/a/17112876/550471 – 2015-03-11 22:36:43

96

你也可以让它出现通过设置α(使之半透明)为禁用。如果你的按钮背景是图像,并且你不想为它创建状态,这是特别有用的。

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

快速,简单,性能友好且不洁净的解决方案。 – 2016-06-13 12:43:59

+4

我已经读过setAlpha的调用在CPU上很贵。有人可以确认吗? – 2016-06-15 07:37:57

+0

@AliKazi它取决于你的'View'的类型。从[本G +文章](https://plus.google.com/+CyrilMottier/posts/DuAZMhFPDjv):“如果您的视图不包含重叠的绘图命令,'setAlpha()'会被优化,我们只需修改Paint (View.hasOverlappingRendering())返回true时,会发生这种情况,没有背景可绘制的'ImageView'和'TextView'是这种优化的常用候选对象,如果您处于这种情况,请使用'setAlpha ()'尽可能多的你想要的。“ – Sufian 2016-06-24 09:43:33

8

所有给出的答案做工精细,但我记得学习,使用setAlpha可以是bad idea性能明智的(更多信息here)。因此创建一个StateListDrawable是一个更好的主意来管理按钮的禁用状态。具体方法如下:

在资源创建一个XML btn_blue.xml /绘制的文件夹:

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

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

创建RES /价值/样式的按钮样式。当你调用btnBlue.setEnabled(true) OR btnBlue.setEnabled(false)状态颜色会自动切换

<Button 
    android:id="@+id/my_disabled_button" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/BlueButton"/> 

现在:XML

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

这种风格,那么应用到您的按钮。

5

你应该创建残疾人按钮XML文件(绘制/ btn_disable.xml

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

而且按钮创建一个选择(绘制/ 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> 
0

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

button.setBackgroundResource(R.drawable.grey); 

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