【问题标题】:(Android) List in dialog with only one item with a checkbox(Android)对话框中的列表只有一个带有复选框的项目
【发布时间】:2012-10-18 18:32:14
【问题描述】:

好的,我已经搜索了一段时间,但我找不到任何东西。我的应用程序在列表中显示学生的学校时间表,当用户单击列表中的课程时,它会显示一个对话框,其中列出了一些选项(编辑、删除、设置警报)。编辑和删除很容易,因为单击它们会发生一些事情,但我需要“设置警报”选项的帮助。我不希望它是可点击的,我只想要它右侧的复选框来打开或关闭警报。这是我的对话框的代码:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(courseName)
    .setItems(R.array.courseList, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch(which) {
            case LIST_EDIT:
                break;
            case LIST_DELETE:
                break;
            case LIST_ALARM:
                break;
            }
        }
    });

    AlertDialog alert = builder.create();
    alert.show();

现在,我的资源 xml 文件中的字符串数组中有列表选项,id 为 courseList。 LIST_EDIT、LIST_DELETE 和 LIST_ALARM 是最终的 int,对应于它们在列表中的索引。我真的不知道如何在警报列表项中添加一个复选框,任何帮助将不胜感激。

【问题讨论】:

    标签: android list checkbox dialog alert


    【解决方案1】:

    如果您熟悉 fragment,则可以使用 FragmentDialog,然后在 onCreateDialog 方法中根据需要创建对话框。

    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.list, null);
    _list = (ListView) view.findViewById(R.id.listview);
    _adapter = new AdapterDialog(_context);
    _list.setAdapter(_adapter);
    //
    AlertDialog.Builder builder = new AlertDialog.Builder(_context);
    builder.setView(view);  builder.setTitle("Dialog").setPositiveButton(getActivity().getString(android.R.string.ok), this);
    
    return builder.create();
    

    或者类似的 然后使用单例方法或构造函数创建对话框

    _dialog = Dialog.newInstance(R.string.title, this);
    _dialog.setCancelable(true);
    _dialog.show(getSupportFragmentManager(), null);
    

    如果你想在列表中有一个自定义行,你不能添加比“按钮”更多的东西,你必须使用适配器

    希望对你有帮助

    【讨论】:

    • 对于仍在关注此内容的任何人,它确实有效。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多