AndroidX-以编程方式模拟首选项

时间:2020-09-30 15:56:15

标签: android androidx android-preferences

我正在将Android项目从android.preference更改为androidx.preference。以前在我的项目中,我使用以下方法以编程方式启动DialogPreference:

public class WaterbodyPreferenceActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        addPreferencesFromResource(R.xml.preferences_waterbody);
        // launch DialogPreference from PreferenceScreen
        PreferenceScreen screen = (PreferenceScreen) findPreference("pref_key_waterBodyInformation");
        waterbodyPickerPreference = (WaterbodyPickerPreference) findPreference("pref_waterBodyName");
        int pos = waterbodyPickerPreference.getOrder();
        screen.onItemClick( null, null, pos, 0 );
    }
}

这很好。不幸的是,screen.onItemClick方法不再可用,我无法找到任何以编程方式启动首选项的方法。

这是我要做什么的完整图片。

我有一个包含多个元素的Fragment,包括一个我想启动DialogPreference的按钮。

正如我所说,当使用android.preference库时,该按钮将启动PreferenceActivity,然后将以编程方式单击一个Preference并使用上面的代码启动DialogPreference。

我尝试在DialogPreference中创建一个show()方法,并从PreferenceFragmentCompat调用它,但这似乎没有任何作用:

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(R.xml.preferences_waterbody);
    WaterbodyPickerPreference dialogPreference = (WaterbodyPickerPreference) findPreference("pref_waterBodyName");
    if (dialogPreference != null) { dialogPreference.show(); }
}

DialogPreference方法:

public void show() {
    onClick();
}

片段布局:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/main_view"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:text="@string/wb_waterbody"
            android:padding="5dp"
            android:textSize="14sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <!-- the button that should launch the DialogPreference -->
        <Button
            android:id="@+id/btn_waterbody"
            android:text="@string/pref_waterBodyName"
            android:background="@color/white"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/btn_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true" >

        <Button
            android:background="@android:drawable/btn_default"
            android:id="@+id/btn_cancel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/btn_cancel" />

        <Button
            android:background="@android:drawable/btn_default"
            android:id="@+id/btn_done"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/btn_done" />

    </LinearLayout>

</RelativeLayout>

preferences_waterbody.xml,我不想显示,只是单击以启动DialogPreference。

<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="pref_key_waterBodyInformation">

    <org.lakeobserver.observer.android.preference.WaterbodyPickerPreference
        android:key="pref_waterBodyName"
        android:dialogTitle="@string/waterbody_picker_title" />

</androidx.preference.PreferenceScreen>

启动PreferenceActivity的片段代码:

btnWaterbody.setOnClickListener(view -> {
    getActivity().getSupportFragmentManager()
            .beginTransaction()
            .replace(android.R.id.content, new WaterbodyPreferenceActivity())
            .commit();
});

更新的PreferenceActivity:

public class WaterbodyPreferenceActivity extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences_waterbody);
        // launch DialogPreference...?
    }
}

1 个答案:

答案 0 :(得分:0)

实际上很简单。

public class WaterbodyPreferenceActivity extends PreferenceFragmentCompat {
    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences_waterbody);
        WaterbodyPickerPreference dialogPreference = (WaterbodyPickerPreference) 
        findPreference("pref_waterBodyName");
        onDisplayPreferenceDialog(dialogPreference);
    }
}
相关问题