2012-01-05 206 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); 

簡單,簡單,但由於使用圖片它獲得更多的空間。