在AlertDialog中是否可以自定义积极和消极按钮?

9

是否可以自定义AlertDialog中的确定和取消按钮?我需要使用自定义样式来替换默认的样式。

.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {...
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {...

能有人告诉我如何做到这一点吗?(涉及IT技术)

3
你需要创建一个自定义对话框。:/ 这是我能找到的最简单的教程。http://i.thiyagaraaj.com/articles/android-articles/customdialogboxpopupusinglayoutinandroid - JustinDanielson
4个回答

6
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);
    }
}
}

FYI button.setBackground() ... 需要 API 级别 16。 - Greg

4

您可以在对话框中设置每个视图。您可以设置带有两个按钮的视图,而不设置积极和消极按钮。

例如:

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

View dialogView = LayoutInflater.from(this)
            .inflate(R.layout.my_layout, null);

builder.setView(dialogView);

1
仅返回翻译文本:超出上下文! - Jivraj S Shekhawat
@Jivraj S Shekhawat 好的,随你便。如果你能提供更好的答案,请尽管提供。 - Roman Black

3
如果您想进行自定义,可以使用对话框而不是警告对话框。以下是示例代码:
    final Dialog dialog = new Dialog(ThisweekActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
    View view = LayoutInflater.from(ThisweekActivity.this).inflate(R.layout.issue_cover_prompt_layout, null);
    view.findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    ImageView img = (ImageView) view.findViewById(R.id.issue_cover_img);
    img.setImageBitmap(issue.getCoverImage());

    dialog.setContentView(view);
    dialog.show();

您可以在对话框中设置布局并添加单击侦听器。


1
如果你想要改变它们,我建议使用带有所需布局的Activity,并将它们添加到你的Activity中,然后在清单文件中声明Dialog。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接