我想知道我怎么知道开关是否打开/关闭以根据开关的状态执行操作
enable_social_recommendations = (Preference) findPreference("enable_social_recommendations");
enable_social_recommendations.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
// here what should i do to know if the switch is on or off???
return true;
}});
答案 0 :(得分:1)
执行以下操作:
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue instanceof Boolean) {
boolean isChecked = (boolean) newValue;
// do whatever you want to do with this
}
return true;
}});
instanceof Boolean
实际上不是必需的,因为newValue
将是布尔值。但这只是为了确保一切都不会发生。
答案 1 :(得分:0)
使用传递给changeListener的newValue参数。