可以在AlertDialog中自定义正负按钮吗?

Dam*_*mir 5 android android-widget

可以在AlertDialog中自定义正负按钮吗?我需要用自定义替换正面和负面的默认外观.

.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {...
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {...
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我该怎么做吗?

小智 5

public class ComentarDialog extends DialogFragment{

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setMessage("Mensaje de alerta")
           .setTitle("Comentar")
           .setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {

               }
           })
           .setNegativeButton("CANCELAR", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {

               }
           });

    return builder.create();
}

@Override
public void onStart() {
    super.onStart();

    //Personalizamos

    Resources res = getResources();

    //Buttons
    Button positive_button =  ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
    positive_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));

    Button negative_button =  ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
    negative_button.setBackground(res.getDrawable(R.drawable.btn_selector_dialog));

    int color = Color.parseColor("#304f5a");

    //Title
    int titleId = res.getIdentifier("alertTitle", "id", "android");
    View title = getDialog().findViewById(titleId);
    if (title != null) {
        ((TextView) title).setTextColor(color);
    }

    //Title divider
    int titleDividerId = res.getIdentifier("titleDivider", "id", "android");
    View titleDivider = getDialog().findViewById(titleDividerId);
    if (titleDivider != null) {
        titleDivider.setBackgroundColor(color);
    }
}
}
Run Code Online (Sandbox Code Playgroud)


AAn*_*kit 1

如果您想更改它们,我建议使用具有所需布局的活动,并在您的活动中的清单文件中添加它们=对话框