警报对话框中的android复选框未显示

adr*_*aes 2 checkbox android android-alertdialog

您好,我正在尝试显示带有复选框的警报对话框,以允许用户选择“不再显示此对话框”选项。对话框正在显示,但复选框没有显示。这是我的代码:

AlertDialog.Builder dialogBack;   
dialogBack = new AlertDialog.Builder(this);
dialogBack.setTitle(context.getString(R.string.msg_attention));
dialogBack.setMessage(context.getString(R.string.msg_photo_caution));
dialogBack.setCancelable(false);

dialogBack.setPositiveButton(context.getString(R.string.confirm_continue),
    new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialogBack, int which) {
            dialogBack.dismiss();

            beginTakeSupervisorPhoto();
        }
    });

dialogBack.setNegativeButton(context.getString(R.string.confirm_cancel),
    new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialogBack, int which) {
            dialogBack.dismiss();

        }
    });


final CharSequence[] items = {context.getString(R.string.msg_dont_show_again)};
dialogBack.setMultiChoiceItems(items, null,
    new DialogInterface.OnMultiChoiceClickListener() {
             public void onClick(DialogInterface dialog, int        indexSelected,boolean isChecked) {
             Log.e("ListaClientesActivity.java","isChecked: "+isChecked);
                 if (isChecked) {
                 showPhotoWarning = false;
                 dataUtil.putBoolean(Constantes.SHOW_PHOTO_WARNING, false);
             }else{
                 showPhotoWarning = true;
                 dataUtil.putBoolean(Constantes.SHOW_PHOTO_WARNING, true);
             }
             dataUtil.savePreferences();

             }
});


dialogBack.create().show();
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为当我在对话框中使用文本视图时它对我有用:

dialogBack.setView(myMsg);
Run Code Online (Sandbox Code Playgroud)

Sai*_*med 5

我的想法只是删除dialogBack.setMessage(context.getString(R.string.msg_photo_caution));并且您的代码运行良好。好像不能同时设置Messageand MultiChoiceItems。您可以通过setView
编辑将您的消息放入标题或将您自己的布局添加到对话框中
用于设置视图的代码:

TextView message = new TextView(context);
message.setText(context.getString(R.string.msg_photo_caution));
CheckBox do_not_show_this_again = new CheckBox(context);
do_not_show_this_again.setText(context.getString(R.string.msg_dont_show_again));
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(message);
layout.addView(do_not_show_this_again);
dialogBack.setView(layout);
Run Code Online (Sandbox Code Playgroud)