在PreferenceFragmentCompat

时间:2019-11-02 23:05:53

标签: java android android-fragments listener preferencefragment

我使用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.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。

2 个答案:

答案 0 :(得分:1)

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

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

希望有帮助!

答案 1 :(得分:0)

经过多番折磨,找到了以下解决方案:(对于AndroidX)

no_of_columns

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

SUM(COUNTIF('X')+COUNTIF('O'))

不要忘了build.gradle依赖项:

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

    }

也许有人会需要))