gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:15:32 28 4
gpt4 key购买 nike

我用的是androidx。在设置 fragment 中,我想创建“首选项”按钮并单击它们以触发一些单独的事件。

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

这是我的一些代码:

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

希望有帮助!

关于java - 如何在 PreferenceFragmentCompat 中点击时触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58676040/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com