Android复选框在警报对话框中未显示



你好,我正在尝试显示一个带有复选框的警报对话框,以便用户选择选项'不要再次显示此对话框'。对话框正在显示,但复选框未显示。这是我的代码:

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

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

dialogBack.setView(myMsg);

我的想法是删除dialogBack.setMessage(context.getString(R.string.msg_photo_caution));,您的代码很好。似乎您无法同时设置MessageMultiChoiceItems。您可以将消息放入标题中,也可以通过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);

您的 items数组应使用整数ID引用,例如R.array.items

至少根据官方指南的示例。

可以解释该元素及其相关的复选框未显示。我只是惊讶于您的IDE没有抓住这一点。

最新更新