2 回答
TA贡献1895条经验 获得超3个赞
找到首选项,然后在其上设置点击侦听器。
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
希望有帮助!
TA贡献1853条经验 获得超9个赞
经过一番折腾,找到解决办法如下:(针对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'
也许有人会需要))
添加回答
举报