首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android :如何在AlertDialog中使用MultiChoiceItems禁用一个项

Android :如何在AlertDialog中使用MultiChoiceItems禁用一个项
EN

Stack Overflow用户
提问于 2016-06-30 11:38:30
回答 1查看 806关注 0票数 1

我用以下代码创建一个对话框:

代码语言:javascript
运行
复制
final CharSequence[] items        = {" One ", " Two ", " Three "};

AlertDialog dialog = new AlertDialog.Builder(this)
            .setTitle("Title1")
            .setMultiChoiceItems(items, null, null)
            .setPositiveButton("CLOSE", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    Log.e("1k", "count : " + ((AlertDialog) dialog).getListView().getChildCount());

                }
            }).show();
    ListView lw = dialog.getListView();
    //lw.getChildAt(0).setEnabled(false);
    Log.e("1k", "count : " + lw.getChildCount());

这将创建一个对话框。当我点击“关闭”按钮时,我可以在日志中看到"3“的输出。到目前为止,"items“数组中有3个字符串。

最后一行代码(在"show()“之后被调用)在日志中给出了"0”。

我想要做的是禁用列表中的第一项,但是这段代码会抛出一个NullPointerException,因为"getChildAt(0)“返回null:

代码语言:javascript
运行
复制
dialog.getListView().getChildAt(0).setEnabled(false);

如何禁用对话框列表中的第一项?

(为什么getChildCount() ..。

。。在show()之后调用时,返回0而不是3?

。。返回3按预期按PositiveButton?)

EN

回答 1

Stack Overflow用户

发布于 2016-06-30 12:31:45

看看下面的代码是否有帮助:

代码语言:javascript
运行
复制
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            MainActivity.this,
            android.R.layout.select_dialog_singlechoice);
    arrayAdapter.add("One");
    arrayAdapter.add("Two");
    arrayAdapter.add("Three");


AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("Title1")
        .setPositiveButton("CLOSE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
        }).show();

dialog.setAdapter(
            arrayAdapter,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int position) {
                    // no event on first position
                    if (position!= 0) {
                    }
                }
            });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38122015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档