【问题标题】:android check box in alert dialog not showing警报对话框中的 android 复选框未显示
【发布时间】:2014-02-18 21:22:08
【问题描述】:

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

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

【问题讨论】:

    标签: android checkbox android-alertdialog


    【解决方案1】:

    我的想法是删除 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);
    

    【讨论】:

    • 是的,我省略了该消息,现在显示该复选框。非常感谢。
    • @adrian4aes 欢迎您。我还通过添加一些代码来更新我的答案
    【解决方案2】:

    您的items 数组应使用R.array.items 之类的整数ID 进行引用

    至少根据official guide 上的示例。

    这可以解释该元素及其关联的复选框未显示。我只是很惊讶您的 IDE 没有捕捉到这一点。

    【讨论】:

    • 我已经更改了这一行 dialogBack.setMultiChoiceItems(R.array.string_array_name, null,... 但这并没有解决我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    相关资源
    最近更新 更多