[英]Android: how to open a ListPreference dialog from outside PreferenceActivity/PreferenceFragment?
I've created a settings menu for my app based on PreferenceFragment
, and would like to access the settings dialog(s) from elsewhere in the app without having to open the settings menu. 我已根据PreferenceFragment
为我的应用程序创建了一个设置菜单,并希望从应用程序的其他位置访问设置对话框,而无需打开设置菜单。
My settings menu has this: 我的设置菜单包含:
and I want to show the same dialog when I click this menu item from the main activity: 当我从主要活动中单击此菜单项时,我想显示相同的对话框:
The main Activity
has one ListFragment
which is where all of the UI handling code is. 主Activity
有一个ListFragment
,它是所有UI处理代码的所在。 Neither is a PreferenceActivity
or PreferenceFragment
. 也不是PreferenceActivity
或PreferenceFragment
。
I just want to invoke the same PreferenceFragment
object to get to the dialog, otherwise I'd have to write custom code to handle the preference changes manually, which I'd like to avoid. 我只是想调用相同的PreferenceFragment
对象来进入对话框,否则我必须编写自定义代码来手动处理首选项更改,我想避免。
I thought adding the PreferenceFragment
to the FragmentManager
in the main Activity
would properly instantiate it, but it doesn't seem to work. 我认为将PreferenceFragment
添加到主Activity
的FragmentManager
会正确实例化它,但它似乎不起作用。
From my menu handler code for the "Sort" option: 从我的菜单处理程序代码“排序”选项:
SettingsFragment fragment = (SettingsFragment) getFragmentManager().findFragmentByTag(SettingsActivity.FRAGMENT_TAG);
// first run case
if (fragment == null) {
fragment = SettingsFragment.newInstance(getActivity());
getFragmentManager().beginTransaction().add(fragment, SettingsActivity.FRAGMENT_TAG).commit();
}
CustomListPreference listPref = (CustomListPreference) fragment.findPreference(SettingsFragment.KEY_PREF_SORTORDER);
listPref.show(); // invokes showDialog(null)
This crashes with a NullPointerException
on listPref
, which shows the PreferenceFragment
was not properly initialized. 这与listPref
上的NullPointerException
崩溃,这表明PreferenceFragment
未正确初始化。
Is there any way to achieve this effect, or do I have to write the functionality as an AlertDialog and manually handle the Preference changes? 有没有办法实现这种效果,还是我必须将功能写为AlertDialog并手动处理首选项更改?
I think you'll have to write this functionality yourself outside of the Preference classes. 我认为你必须自己在Preference类之外编写这个功能。
Preference, PreferenceActivity, and PreferenceFragment were all designed to work together to provide a consistent UIX for android apps. Preference,PreferenceActivity和PreferenceFragment都旨在协同工作,为Android应用程序提供一致的UIX。 As such it's recommended to use them together as they were intended. 因此,建议按原样使用它们。
You can't directly replicate the UI of PreferenceActivity or PreferenceFragment outside of those two classes in a regular activity because the UI is built from Preference objects, not View objects like regular Activities. 您不能直接在常规活动中复制这两个类之外的PreferenceActivity或PreferenceFragment的UI,因为UI是从Preference对象构建的,而不是像常规活动这样的View对象。 So if you want that particular UI you'd have to try and duplicate it using custom Views. 因此,如果您想要特定的UI,则必须尝试使用自定义视图复制它。
https://discussions.udacity.com/t/way-to-do-listpreference-outside-of-settings-menu/45473 https://discussions.udacity.com/t/way-to-do-listpreference-outside-of-settings-menu/45473
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.