Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何自定义AlertDialog的默认按钮?_Android_Android Alertdialog_Android Dialog - Fatal编程技术网

Android 如何自定义AlertDialog的默认按钮?

Android 如何自定义AlertDialog的默认按钮?,android,android-alertdialog,android-dialog,Android,Android Alertdialog,Android Dialog,问题是,我有一个警报对话框,在其中我通过以下方式膨胀自定义布局: AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity()); View mView = getLayoutInflater().inflate(R.layout.exp_add, null); mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

问题是,我有一个
警报对话框
,在其中我通过以下方式膨胀自定义布局:

AlertDialog.Builder mBuilder = new AlertDialog.Builder(getActivity());
View mView  = getLayoutInflater().inflate(R.layout.exp_add, null);
mBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) { 
                         //some of my data and mView manipulation
                    });


mBuilder.setView(mView);
AlertDialog dialog = mBuilder.create();
dialog.show();
问题是如何自定义
.setPositiveButton
,因为我有一个具有特定背景颜色的视图,但mBuilder添加了一个具有默认白色背景颜色的按钮和一个粉色文本按钮


有没有办法自定义此按钮?

我认为
对话框界面。按钮为中性的
必须是
对话框界面。按钮为阴性的
AlertDialog.Builder builder = new AlertDialog.Builder(context);
    LayoutInflater inflater = context.getLayoutInflater();

    //setting custom view for our dialog
    View myview = inflater.inflate(R.layout.YOUR_CUSTOM_LAYOUT, null);
    builder.setNeutralButton(android.R.string.cancel, null);
    builder.setView(myview);

    //creating an alert dialog from our builder.
    AlertDialog dialog = builder.create();
    dialog.show();

    //retrieving the button view in order to handle it.
    Button neutral_button = dialog.getButton(DialogInterface.BUTTON_NEUTRAL);

    Button positive_button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);


    if (neutral_button != null) {
        neutral_button.setBackgroundDrawable(context.getResources()
                        .getDrawable(R.drawable.custom_background));

        neutral_button.setTextColor(context.getResources()
                        .getColor(android.R.color.white));
    }
    if (positive_button != null) {
        positive_button.setBackgroundDrawable(context.getResources()
                        .getDrawable(R.drawable.custom_background));

        positive_button.setTextColor(context.getResources()
                        .getColor(android.R.color.white));
    }