首页 > 解决方案 > AndroidX - 以编程方式模拟偏好点击

问题描述

我正在将我的 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 方法不再可用,我无法找到以编程方式启动首选项的任何方法。

这是我正在尝试做的更完整的图片。

我有一个包含多个元素的片段,包括一个我想启动 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...?
    }
}

标签: androidandroidxandroid-preferences

解决方案


其实很简单。

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);
    }
}

推荐阅读