2010-11-04 205 views
30

如何在Android中的AlertDialog中更改按鈕的顏色?更改AlertDialog中的按鈕顏色

+0

看看這個[Android的對話與自定義顏色和設計(http://stackoverflow.com/a/30388711/782535) 。 – 2015-05-22 05:08:53

回答

-1

你指的是中性,正面和負面的按鈕?或者您在佈局中包含的按鈕?

如果你指的是前者,那麼你可以。檢查出Custom Button section in this tutorial。你基本上需要一個XML文件,它會告訴你的按鈕每個狀態改變使用哪種drawable/color。然後,您可以將此XML文件設置爲按鈕的背景。

+0

沒有budy,我已經做了那個。但我必須改變正面,中性和負面按鈕的背景。 。 。 – 2011-04-30 07:29:34

+0

嗨,你是否設法解決這個問題? – jonney 2011-07-20 09:55:22

0

不,你不能改變警告框的默認按鈕的顏色或圖像或背景。 對於自定義,您需要讓您在自定義對話框中這樣。

public class TryAgainAlert extends Dialog implements OnClickListener 
{ 
    @Override 
public boolean onKeyDown(int keyCode, KeyEvent event) 
{ 
    if (keyCode == KeyEvent.KEYCODE_BACK) 
    { 

    Intent i = new Intent(getApplicationContext(), MainMenu.class); 
    finish(); 
    startActivity(i); 

    return true; 
    } 
    return super.onKeyDown(keyCode, event); 
} 


    TextView scores; 
    Button tryagain,mainmenu,submit; 


    public TryAgainAlert(Context context) { 
     super(context); 

     setContentView(R.layout.tryagainalert); 

     scores=(TextView)findViewById(R.id.text); 



     tryagain= (Button) findViewById(R.id.trya); 
     mainmenu= (Button) findViewById(R.id.submitscore); 
     submit= (Button) findViewById(R.id.mainmenu); 

    } 


    @Override 
    public void onClick(View v) { 
     if(v == tryagain) 
     { 

     else if (v==mainmenu) 
     { 


     } 
     else if (v == submit) 
     { 

     } 
    } 

} 

你可以做任何你想要的XML文件。 我希望這會有所幫助。 謝謝

60

這是我做的。

AlertDialog.Builder customBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this,android.R.style.Theme_Dialog)); 

customBuilder.setTitle(R.string.popup_error_title); 
customBuilder.setNegativeButton("Exit application", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
     MyActivity.this.finish(); 
    } 
}); 

AlertDialog dialog = customBuilder.create(); 
dialog.show(); 

Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE); 

if(b != null) { 
    b.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button)); 
} 

我找到繪製here

+2

嗨,我也是這樣應用Button的BG圖像。但如果我申請圖片我得到一些不必要的邊緣在按鈕的底部。如果我使用BG顏色,我工作的很好。我檢查了按鈕圖像。很好。所以你可以爲此提出任何解決方案。 – Raj 2012-10-09 07:49:31

+0

@Raj你有沒有找到這種不需要的利潤率的解決方案? – Deepak 2012-10-10 19:41:05

+0

@Deepak:尚未找到任何解決方案。如果你發現任何請讓我知道。 – Raj 2012-10-11 05:25:38

7

下面是一些例子:

AlertDialog.Builder b = new AlertDialog.Builder(all.this); 

b.setMessage("r u wan't 2 exit"); 
b.setCancelable(false); 

b.setNegativeButton("no", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
     dialog.cancel();  
    } 
}); 

b.setPositiveButton("yes", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
     Intent i=new Intent(getBaseContext(), s.class); 
     startActivity(i); 
    } 
}); 

AlertDialog a=b.create(); 

a.show(); 

Button bq = a.getButton(DialogInterface.BUTTON_NEGATIVE); 
bq.setBackgroundColor(Color.BLUE); 
18

由於大多數人可能使用DialogFragment現在我遇到了一些問題,並點擊我的方式,通過幾個SO解決這些問題的答案。讓我發表我目前的解決方案。

我已經結束了自定義drawables的設置按鈕背景已經多次建議。但是,這在onCreateDialog- DialogFragment的方法中尚不可行。你可以這樣做,例如在onStart()中,或者(這是我首選的)onShow-對話框的聽衆!請記住,您需要在更改後使您的按鈕無效。

至於邊距:簡單地刪除您的Drawable-XML中的按鈕的填充。

#onCreateDialog在DialogFragment:一個按鈕

@Override 
public Dialog onCreateDialog(final Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    // setup your dialog here... 

    builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(final DialogInterface dialog, final int which) { 
     // do something 
    } 
    }); 

    builder.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(final DialogInterface dialog, final int which) { 
     // do something 
    } 
    }); 

    final AlertDialog dialog = builder.create(); 

    dialog.setOnShowListener(new DialogInterface.OnShowListener() { 
    @Override 
    public void onShow(final DialogInterface dialog) { 
     Button negativeButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_NEGATIVE); 
     Button positiveButton = ((AlertDialog)dialog).getButton(DialogInterface.BUTTON_POSITIVE); 

     // this not working because multiplying white background (e.g. Holo Light) has no effect 
     //negativeButton.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY); 

     final Drawable negativeButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_red); 
     final Drawable positiveButtonDrawable = getResources().getDrawable(R.drawable.alert_dialog_button_light_green); 
     if (Build.VERSION.SDK_INT >= 16) { 
     negativeButton.setBackground(negativeButtonDrawable); 
     positiveButton.setBackground(positiveButtonDrawable); 
     } else { 
     negativeButton.setBackgroundDrawable(negativeButtonDrawable); 
     positiveButton.setBackgroundDrawable(positiveButtonDrawable); 
     } 

     negativeButton.invalidate(); 
     positiveButton.invalidate(); 
    } 
    }); 

    return dialog; 
} 

繪製對象,XML例如:

<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_pressed="true" > 
    <shape> 
     <gradient 
     android:startColor="@color/alert_dialog_button_green_pressed1" 
     android:endColor="@color/alert_dialog_button_green_pressed2" 
     android:angle="270" /> 
    </shape> 
    </item> 

    <item android:state_focused="true" > 
    <shape> 
     <gradient 
     android:endColor="@color/alert_dialog_button_green_focused1" 
     android:startColor="@color/alert_dialog_button_green_focused2" 
     android:angle="270" /> 
    </shape> 
    </item> 

    <item> 
    <shape> 
     <gradient 
     android:endColor="@color/alert_dialog_button_green1" 
     android:startColor="@color/alert_dialog_button_green2" 
     android:angle="270" /> 
    </shape> 
    </item> 
</selector> 

不要忘了在res\values\colors.xml定義顏色,例如像這樣(我不想一個梯度,因此顏色1 & 2相同):

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <color name="alert_dialog_button_green1">#b4099930</color> 
    <color name="alert_dialog_button_green2">#b4099930</color> 
    <color name="alert_dialog_button_green_focused1">#96099930</color> 
    <color name="alert_dialog_button_green_focused2">#96099930</color> 
    <color name="alert_dialog_button_green_pressed1">#96099930</color> 
    <color name="alert_dialog_button_green_pressed2">#96099930</color> 
</resources> 
1

我認爲有開發商誰願意延長AlertDialog類,並定義的按鈕顏色withing類定義。對於這樣的目的,你可以使用此代碼:

class MyDialog extends AlertDialog { 
    public MyDialog(final Context context) { 
     super(context); 
     setOnShowListener(new OnShowListener() { 
      @Override 
      public void onShow(DialogInterface dialog) { 
       Button negativeButton = getButton(DialogInterface.BUTTON_NEGATIVE); 
       Button positiveButton = getButton(DialogInterface.BUTTON_POSITIVE); 

       negativeButton.setBackgroundColor(Color.GREEN); 
       positiveButton.setBackgroundColor(Color.RED); 
      } 
     }); 
    } 
} 
6

我已經通過這個代碼做它可以幫助你:

AlertDialog.Builder builder1 = new AlertDialog.Builder(this); 
     builder1.setCancelable(true); 
    builder1.setTitle("abc"); 
     builder1.setMessage("abcdefg"); 
     builder1.setInverseBackgroundForced(true); 
    builder1.setPositiveButton("Yes", 
      new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      dialog.cancel(); 
     } 
    }); 

    builder1.setNegativeButton("No", 
      new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      dialog.cancel(); 
     } 
    }); 

    AlertDialog alert11 = builder1.create(); 
    alert11.show(); 

    Button buttonbackground = alert11.getButton(DialogInterface.BUTTON_NEGATIVE); 
    buttonbackground.setBackgroundColor(Color.BLUE); 

    Button buttonbackground1 = alert11.getButton(DialogInterface.BUTTON_POSITIVE); 
    buttonbackground1.setBackgroundColor(Color.BLUE); 
0

要更改AlertDailog

代碼的按鈕顏色:

// Initialize AlertDialog & AlertDialog Builder 
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this); 
builder.setTitle(R.String.AlertDialogTitle); 
........... 
......... 
//Build your AlertDialog 
AlertDialog Demo_alertDialog= builder.create(); 
Demo_alertDialog.show(); 

//For Positive Button: 
Button b_pos; 
b_pos=Demo_alertDialog.getButton(DialogInterface.BUTTON_POSITIVE); 
if(b_pos!=null){ 
    b_pos.setTextColor(getResources().getColor(R.color.YourColor)); 
    }  


//For Neutral Button: 
Button b_neu; 
b_neu=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL); 
if(b_neu!=null){ 
    b_neu.setTextColor(getResources().getColor(R.color.YourColor)); 
    } 

//For Negative Button: 
Button b_neg; 
b_neg=Demo_alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE); 
if(b_neg!=null){ 
    b_neg.setTextColor(getResources().getColor(R.color.YourColor)); 
    } 
0

按鈕和其他文字的顏色也可以使用appcompat更改:

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert"> 
    <item name="android:colorPrimary">@color/flexdrive_blue_1</item> 
    <item name="android:textColorPrimary">@color/flexdrive_blue_6</item> 
    <item name="android:colorAccent">@color/flexdrive_blue_1</item> 
    <item name="colorPrimaryDark">@color/flexdrive_blue_4</item> 
</style> 
0

如果你正在使用DialogFragment(android.app.DialogFragment),那麼你可以覆蓋OnStart方法來獲得所有的按鈕(正,負和中性)的手柄。

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    View eventEditDialogView = View.inflate(this.getActivity(), R.layout.event_edit_dialog, 
              null); 

    builder.setTitle(getLocalizedString("edit_event")) 
      .setView(eventEditDialogView) 
      .setPositiveButton(getLocalizedString("all_events"), new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
       } 
      }) 
      .setNegativeButton(getLocalizedString("this_event"), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
       } 
      }) 
    return builder.create(); 
} 

@Override 
    public void onStart() { 
     super.onStart(); 
    Button positive = ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE); 
    positive.setTextColor(Color.BLACK); 
    positive.setBackgroundColor(getResources().getColor(R.color.GrayBGColor)); 
} 

所有上述解決方案將與AlertDialog或對話工作在同一活動或片段創建,但在DialogFragment單獨創建。

3

我想用主題而不是額外的代碼來解決這個問題,因爲我覺得在styles.xml中擁有所有與樣式相關的東西更加清晰。我所做的是基於Arade的回答和this other question

<style name="AlertDialogDanger" parent="Theme.AppCompat.Light.Dialog.Alert"> 
    <item name="colorAccent">@color/error</item> 
</style> 

這將改變你的風格AlertDialogDanger創建任何警告對話框的按鈕文本的顏色。要做到這一點:

new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDialogDanger)) 
      .setMessage("Really delete?") 
      .setPositiveButton("Delete", null) 
      .setNegativeButton("Cancel", null) 
      .create().show(); 
2

我們可以使用樣式更改警報對話框按鈕的文字顏色。

AlertDialog.Builder dialog = new AlertDialog.Builder(context, R.style.yourDialog); 
    dialog.setTitle(R.string.title); 
    dialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      //code here 
     } 
    }); 
    dialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialogInterface, int i) { 
      //do here 
     } 
    }); 

    dialog.show(); 

Style.xml

<style name="yourDialog" parent="Theme.AppCompat.Light.Dialog.Alert"> 

    <item name="android:colorAccent">@color/themeColor</item> 
    <item name="android:colorPrimary">@color/themeColor</item> 

</style> 
0
//el resto 
    AlertDialog a=alertDialog.create(); 
    cambiar_color_texto_alertdialog(a); 

} 

public void cambiar_color_texto_alertdialog(AlertDialog a){ 
    a.show(); 
    Button BN = a.getButton(DialogInterface.BUTTON_NEGATIVE); 
    BN.setTextColor(parseColor("#2E9AFE")); 
    Button BA = a.getButton(DialogInterface.BUTTON_POSITIVE); 
    BA.setTextColor(parseColor("#2E9AFE")); 
}