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

我用的是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"

    </PreferenceCategory>


</PreferenceScreen>

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


撒科打诨
浏览 139回答 2
2回答

蛊毒传说

找到首选项,然后在其上设置点击侦听器。public class SettingsFragment extends PreferenceFragmentCompat {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {&nbsp; &nbsp; &nbsp; &nbsp; addPreferencesFromResource(R.xml.preference);&nbsp; &nbsp; &nbsp; &nbsp; Preference preference = findPreference("button1");&nbsp; &nbsp; &nbsp; &nbsp; preference.setOnClickListener(new OnPreferenceClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean onPreferenceClick(Preference p) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Handle preference click&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }}请查看有关使用首选项的文档: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 {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; addPreferencesFromResource(R.xml.preference);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Preference preferenceMap = findPreference("button");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; preferenceMap.setOnPreferenceClickListener(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Preference.OnPreferenceClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public boolean onPreferenceClick(Preference arg0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getActivity().onBackPressed();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ((MainActivity) getActivity()).injectSetting("map");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; View view = super.onCreateView(inflater, container, savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; view.setBackgroundColor(getResources().getColor(R.color.graylight));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toolbar.setTitle(R.string.action_settings);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; toolbar.setLogo(R.drawable.ic_settings_white_24dp);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return view;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }并在 XML androidx.preference 中添加到元素中:<?xml version="1.0" encoding="utf-8"?><androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto">&nbsp; &nbsp; <androidx.preference.PreferenceCategory&nbsp; &nbsp; &nbsp; &nbsp; android:title="@string/setting_person"&nbsp; &nbsp; &nbsp; &nbsp; app:initialExpandedChildrenCount="1"&nbsp; &nbsp; &nbsp; &nbsp; app:key="profile_set">&nbsp; &nbsp; &nbsp; &nbsp; <androidx.preference.Preference&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/preference1"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:key="button"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:summary="@string/setting_person_data"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:title="@string/setting_person_reg"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:icon="@drawable/ic_assignment" />&nbsp; &nbsp; &nbsp; &nbsp; <androidx.preference.Preference&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/preference2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:key="button2"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:summary="@string/setting_avatar"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:title="@string/setting_avatar_chg"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app:icon="@drawable/ic_wallpaper_black_24dp" />&nbsp; &nbsp; </androidx.preference.PreferenceCategory></androidx.preference.PreferenceScreen>并且不要忘记 build.gradle 依赖项:implementation 'androidx.preference:preference:1.1.0'也许有人会需要))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java