【发布时间】:2016-06-30 11:38:30
【问题描述】:
我用以下代码创建了一个对话框:
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:
dialog.getListView().getChildAt(0).setEnabled(false);
如何禁用对话框列表中的第一个项目?
(以及为什么 getChildCount() ..
.. 在 show() 之后调用时返回 0 而不是 3?
.. 在 PositiveButton 的 onclick 中按预期返回 3 ? )
【问题讨论】:
-
而不是
setMultiChoiceItems在您的适配器实现中使用setAdapter... 覆盖ListAdapter接口 ... -
我试过这个,但外观和感觉不是原生的,即使使用 android.R.layout.select_dialog_multichoice 作为布局(我猜这不是材料之一;视图是方式太大了。即使我找到 Lollipop+ 使用的正确布局,它也不会向前兼容)