简体   繁体   English

Android:如何从PreferenceActivity / PreferenceFragment外部打开ListPreference对话框?

[英]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 . 也不是PreferenceActivityPreferenceFragment

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添加到主ActivityFragmentManager会正确实例化它,但它似乎不起作用。

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.

相关问题 Android创建没有PreferenceActivity的PreferenceFragment - Android create a PreferenceFragment without a PreferenceActivity Android - 使用PreferenceFragment的PreferenceActivity中的标头类别 - Android - Headers categories in PreferenceActivity with PreferenceFragment 如何从 PreferenceActivity 调用特定的 PreferenceFragment? - How do I call a specific PreferenceFragment from a PreferenceActivity? 安卓 PreferenceActivity。 ListPreference。 如何更改ListPreference的背景颜色? - Android. PreferenceActivity. ListPreference. How can I change the backgrounds color of ListPreference? 首选项活动列表首选项 - PreferenceActivity Listpreference Android-SharedPreferences ListPreference-PreferenceFragment onCreate未调用 - Android - SharedPreferences ListPreference - PreferenceFragment onCreate not called 如何将设备保护存储用于 PreferenceActivity 和 PreferenceFragment - How to use Device Protected Storage for PreferenceActivity and PreferenceFragment Android ListPreference条目文本,而不是不扩展PreferenceFragment的Activity或Fragment中的条目值 - Android ListPreference entry text instead of entry value from Activity or Fragment that doesn't extend PreferenceFragment 如何从PreferenceFragment显示对话框? - How can I display a Dialog from a PreferenceFragment? Android PreferenceActivity和对话框片段 - Android PreferenceActivity and dialog fragments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM