是否可以在AlertDialog中自定义肯定和否定按钮? [英] Is possible to customize positive and negative buttons in AlertDialog?

查看:80
本文介绍了是否可以在AlertDialog中自定义肯定和否定按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is possible to customize positive and negative buttons in AlertDialog ? I need to replace default look of positive and negative with custom.

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

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

Can somebody tell me how to do that ?

推荐答案

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

这篇关于是否可以在AlertDialog中自定义肯定和否定按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆