如何在 PreferenceFragmentCompat 中单击时触发事件

How to trigger an event when clicked in PreferenceFragmentCompat

我用的是androidx。在设置片段中,我想创建 "Preference" 按钮并单击它们以触发一些单独的事件。

如何在特定首选项上实现点击监听器?

这就是我的一些代码:

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

        addPreferencesFromResource(R.xml.preference);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = super.onCreateView(inflater, container, savedInstanceState);
        view.setBackgroundColor(getResources().getColor(R.color.graylight));

        Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.action_settings);
        toolbar.setLogo(R.drawable.ic_settings_white_24dp);

        PreferenceManager preferenceManager = getPreferenceManager();
        PreferenceScreen preferenceScreen = getPreferenceScreen();

        return view;
    }

}

和XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory
        android:title="@string/setting_person"
        app:initialExpandedChildrenCount="0"
        app:key="profile_set">

        <Preference
            android:id="@+id/preference2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:key="button1"
            android:summary="@string/setting_person_data"
            android:title="@string/setting_person_reg"
            app:icon="@drawable/ic_assignment" />
        <Preference
            android:key="button"
            android:summary="@string/setting_avatar"
            android:title="@string/setting_avatar_chg"
            app:icon="@drawable/ic_wallpaper_black_24dp" />

    </PreferenceCategory>

</PreferenceScreen>

结果我想在MainActivity中点击触发一个事件。但这是另一个问题,现在至少我应该听听事件,例如通过按下按钮的键调用 Toast。

找到首选项,然后在其上设置点击侦听器

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

        addPreferencesFromResource(R.xml.preference);

        Preference preference = findPreference("button1");
        preference.setOnClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference p) {
                // Handle preference click
            }
        });
    }
}

请查看有关使用首选项的文档:

https://developer.android.com/reference/androidx/preference/package-summary.html https://developer.android.com/reference/androidx/preference/PreferenceFragmentCompat.html https://developer.android.com/reference/androidx/preference/PreferenceFragmentCompat.html#findPreference(java.lang.CharSequence) https://developer.android.com/reference/androidx/preference/Preference.OnPreferenceClickListener.html

希望对您有所帮助!

折腾了半天,找到解决方法如下:(AndroidX)

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.widget.Toolbar;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

public class SettingsFragment extends PreferenceFragmentCompat {

        @Override
        public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

            addPreferencesFromResource(R.xml.preference);

            Preference preferenceMap = findPreference("button");

            preferenceMap.setOnPreferenceClickListener(
                    new Preference.OnPreferenceClickListener() {
                        @Override
                        public boolean onPreferenceClick(Preference arg0) {
                            getActivity().onBackPressed();
                            ((MainActivity) getActivity()).injectSetting("map");
                            return true;
                        }
                    });

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View view = super.onCreateView(inflater, container, savedInstanceState);
            view.setBackgroundColor(getResources().getColor(R.color.graylight));

            Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
            toolbar.setTitle(R.string.action_settings);
            toolbar.setLogo(R.drawable.ic_settings_white_24dp);

            return view;
        }

    }

并在XML中添加了androidx.preference元素:

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

    <androidx.preference.PreferenceCategory
        android:title="@string/setting_person"
        app:initialExpandedChildrenCount="1"
        app:key="profile_set">

        <androidx.preference.Preference
            android:id="@+id/preference1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:key="button"
            android:summary="@string/setting_person_data"
            android:title="@string/setting_person_reg"
            app:icon="@drawable/ic_assignment" />
        <androidx.preference.Preference
            android:id="@+id/preference2"
            android:key="button2"
            android:summary="@string/setting_avatar"
            android:title="@string/setting_avatar_chg"
            app:icon="@drawable/ic_wallpaper_black_24dp" />
    </androidx.preference.PreferenceCategory>

</androidx.preference.PreferenceScreen>

并且不要忘记 build.gradle 依赖项:

implementation 'androidx.preference:preference:1.1.0'

也许有人需要 ))