在对话框中更改按钮文本的颜色

时间:2018-09-05 10:17:50

标签: java android colors

我正在尝试更改“ Impostazioni”和“ Chiudi”的颜色,但是我不能,该怎么办?

public class noNetworkConnection extends DialogFragment{

private String Titolo = "Errore";
private String Messaggio = "Sembra che tu non sia connesso a internet!";
private String Impostazioni = "Impostazioni";
private String Chiudi = "Chiudi";

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setMessage(Messaggio).setTitle(Titolo).setIcon(R.mipmap.ic_connection_error_nointernetconnecction)
                            .setPositiveButton(Impostazioni, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
                }
            })
            .setNegativeButton(Chiudi, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    System.exit(0);
                }
            });
    // Create the AlertDialog object and return it
    return builder.create();
  }

}

3 个答案:

答案 0 :(得分:0)

Button buttonPositive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
buttonPositive.setTextColor(ContextCompat.getColor(this, R.color.green));
Button buttonNegative = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
buttonNegative.setTextColor(ContextCompat.getColor(this, R.color.red));

答案 1 :(得分:0)

public void createDialog(final Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(Messaggio).setTitle(Titolo).setIcon(R.mipmap.ic_connection_error_nointernetconnecction)
                .setPositiveButton(Impostazioni, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
                    }
                })
                .setNegativeButton(Chiudi, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        System.exit(0);
                    }
                });

        AlertDialog alert = builder.create();
        alert.show();
        Button negbutton = alert.getButton(DialogInterface.BUTTON_NEGATIVE);
        negbutton.setTextColor(getResources().getColor(R.color.green));
        Button posButton = alert.getButton(DialogInterface.BUTTON_POSITIVE);
        posButton.setTextColor(getResources().getColor(R.color.red));
    }

答案 2 :(得分:0)

控制文本样式的最简单方法是使用HTML属性

builder.setPositiveButton(Html.fromHtml("<font color='#FF4081'>OK</font>"), null);