我有一个preference.xml 文件,我想显示或隐藏这个文件的一些条目。但问题是偏好没有被隐藏。我的代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Preference customPref = (Preference) findPreference(getText(R.string.about));
customPref.setOnPreferenceClickListener(this);
Preference termAndConditions = (Preference) findPreference(getText(R.string.term_and_conditions));
termAndConditions.setOnPreferenceClickListener(this);
Preference privacyStatement = (Preference) findPreference(getText(R.string.privacy_statement));
privacyStatement.setOnPreferenceClickListener(this);
Preference login_with_facebook = (Preference) findPreference(getText(R.string.login_with_facebook));
login_with_facebook.setOnPreferenceClickListener(this);
Preference register_new_account = (Preference) findPreference(getText(R.string.register_new_account));
register_new_account.setOnPreferenceClickListener(this);
Preference login = (Preference) findPreference(getText(R.string.login));
login.setOnPreferenceClickListener(this);
Preference my = (Preference) findPreference(getText(R.string.my));
my.setOnPreferenceClickListener(this);
Preference miles_and_more = (Preference) findPreference(getText(R.string.miles_and_more));
miles_and_more.setOnPreferenceClickListener(this);
Preference voucher_booking = (Preference) findPreference(getText(R.string.voucher_booking));
voucher_booking.setOnPreferenceClickListener(this);
Preference invite_friends = (Preference) findPreference(getText(R.string.invite_friends));
invite_friends.setOnPreferenceClickListener(this);
}
onResume{
buildLogin()
}
private void buildLogin() {
PreferenceScreen preferenceScreen = getPreferenceScreen();
hidePreferences(preferenceScreen, R.string.my);
hidePreferences(preferenceScreen, R.string.miles_and_more);
hidePreferences(preferenceScreen, R.string.voucher_booking);
hidePreferences(preferenceScreen, R.string.invite_friends);
}
private void hidePreferences(PreferenceScreen preferenceScreen,
int preferenceKey) {
Preference customPref = (Preference) findPreference(getText(preferenceKey));
preferenceScreen.removePreference(customPref);
}
这个想法是隐藏 onResume() 上的一些偏好。这个类是从 PreferenceFragment 扩展而来的。有人可以指出我做错了什么。没有任何偏好是隐藏的。