是否可以从DialogFragment构建DialogPreference?

时间:2013-06-25 15:13:09

标签: android android-preferences

我已将目录选择器实现为DialogFragment,现在我想在DialogPreference中使用它。可能吗?怎么做?

1 个答案:

答案 0 :(得分:0)

对我来说,确切要执行的操作并不明显,但是我假设您要在“偏好设置”点击中显示“片段”布局。这是它的代码:

preferences.xml

<PreferenceScreen ... >
    ...
    <Preference android:title="Dialog Fragment Displayer"
        android:key="dialog_preference"
        android:selectable="true"/>
    ...
</PreferenceScreen>

PreferenceActivity.java (或您的主要设置控件位于何处)

final Activity activity = getActivity(); //replace with this if in an Activity
Preference p = findPreference("dialog_preference");
p.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
    @Override
    public boolean OnPreferenceClick(Preference preference) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        View view = activity.getLayoutInflater().inflate(R.layout.your_fragment, null, false);
        builder.setView(view);
        builder.create().show();
    }
});