如何创建一个ListPreference对话框以在非Preference活动中显示

4

我希望在非Preference Activity中显示ListPreference对话框。

这是创建对话框的代码:

// this is my current activity
ListPreference listPreference = new ListPreference(this);   
listPreference.setDialogIcon(R.drawable.auto_download);
listPreference.setTitle(R.string.autoDownloadTitle);
listPreference.setEntries(R.array.autoDownloadEntries);
listPreference.setEntryValues(R.array.autoDownloadValues);
listPreference.setNegativeButtonText(R.string.cancel);
Dialog dialog = listPreference.getDialog();

我的问题是对话框始终为空,我错过了什么吗?

1个回答

1
你不能这样做,Preference 应该在 PreferenceScreen 中,并且活动必须扩展 PreferenceActivity
我认为你想要实现一个显示选项列表的对话框,就像 ListPreference 一样。我建议使用基于 ArrayAdapter 的列表视图的 AlertDialog
请查看 this 以获取示例或 AlertDialog.Builder 文档。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接