我遇到几个无法解析符号错误。还说我的return true
语句在方法之外。我已经检查了缩进,并尝试import androidx.preference.Preference.OnPreferenceClickListener;
但从未使用过导入。请帮忙
在setOnPreference
clicklistener上显示红色,在onPreferenceClick中显示首选项参数。
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preferences);
}
Preference cache = (Preference) findPreference(getString(R.string.clear_cache));
cache.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
//open browser or intent here
Toast.makeText(requireContext(), "Test!",
Toast.LENGTH_LONG).show();
return true;
}
});
Preference logout = (Preference) findPreference(getString(R.string.logout));
logout.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
//open browser or intent here
Intent intent = new Intent(getActivity(),
LoginActivity.class);
startActivity(intent);
return true;
}
});
}