2016-10-08 3 views
1

Учитывая функциюИзменения цвета кнопки в AlertDialog.Builder

private void showInputDialog() { 
    final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); 
    final EditText input = new EditText(getActivity()); 
    input.setSingleLine(); 
    FrameLayout container = new FrameLayout(getActivity()); 
    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    params.leftMargin= convertDpToPx(25);          //remember to scale correctly 
    params.rightMargin= convertDpToPx(30); 
    input.setLayoutParams(params); 
    container.addView(input); 
    alert.setTitle("Change City"); 
    alert.setMessage("Hey there, could not find the city you wanted. Please enter a new one:\n"); 
    alert.setView(container); 
    alert.setPositiveButton("Go", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      changeCity(input.getText().toString()); 
     } 
    }); 
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.cancel(); 
     } 
    }); 
    alert.show(); 
} 

Теперь, когда я получаю эту AlertDialog.Builder в моем приложении, кнопка цвет зеленый (по умолчанию от Android 5), но цвет EditText Розовый (R.color.coloraccent). Как изменить цвет кнопок на розовый?

Любая помощь будет оценена

+0

установить стиль в alerdialog. –

ответ

7

попробовать, как этот

alertDialog.show(); 

Только после .show(). попробовать этот фрагмент

//for negative side button 
    alertDialog.getButton(dialog.BUTTON_NEGATIVE).setTextColor(neededColor); 
//for positive side button 
    alertDialog.getButton(dialog.BUTTON_POSITIVE).setTextColor(neededColor); 
+0

Это сработало для меня. Спасибо, сэр! – Sparker0i

+0

welcome ... happy coding –

0

вам нужно добавить пользовательское представление в вашем окне предупреждения. создает файл макета с именем «my_custom_alert_window», то сделать, как показано ниже:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
LayoutInflater inflater = this.getLayoutInflater(); 
View dialogView = inflater.inflate(R.layout.my_custom_alert_window, null); 
dialogBuilder.setView(dialogView); 

Button btn = (Button) dialogView.findViewById(R.id.label_field); 
btn.setBackgroundColor(....); 
AlertDialog alertDialog = dialogBuilder.create(); 
alertDialog.show(); 

, то вы можете изменить цвет фона кнопки, как выше

3

Вы можете тему этого. Кнопки использовать accentColor в теме по умолчанию для TextColor, как вы отметили, чтобы изменить это, вы должны:

Измените тему приложения в Рез/значений/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    ... 
    <item name="buttonBarPositiveButtonStyle">@style/Base.Widget.AppCompat.Button.Borderless.MyStyle</item> 
</style> 

И добавить кнопку стиль темы кнопка AlertDialog

<style name="Base.Widget.AppCompat.Button.Borderless.MyStyle"> 
    <item name="android:textColor">@color/colorAccent</item> 
</style> 

Примечание

Это затронет все AlertDialogs и не только один. Вы также можете создать новую отдельную тему и добавить к AlertDialog.Builder как new AlertDialog.Builder(getActivity(), R.style.MyAlertDialogTheme)

+0

Это не будет работать, поскольку AlertDialog использует '? AlertDialogTheme'' AppTheme'. Вам придется переопределить тему диалога предупреждения и стиль кнопки внутри него. –

Смежные вопросы