我用以下代码创建一个对话框:
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?
。。返回3按预期按PositiveButton?)
发布于 2016-06-30 12:31:45
看看下面的代码是否有帮助:
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) {
}
}
});
https://stackoverflow.com/questions/38122015
复制相似问题