9

我已经将目录选择器实现为 a DialogFragment,现在我想在DialogPreference. 可能吗?怎么做?

4

1 回答 1

0

对我来说,你到底想做什么并不明显,但我假设你有一个片段布局,你想在首选项点击时显示它。这是它的代码:

首选项.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();
    }
});
于 2020-11-28T16:00:34.773 回答