有没有办法在 API11 中处理 OnPreferenceChangeListener。简单地调用 Preference.setOnPreferenceChangeListener 会引发一个 lint 错误,说明它需要 API14+,但是我的应用程序是为 API11+ 设计的,我不想限制它可以进一步运行的设备数量。
更新:我已经检查了开发文档,这应该可以从 API1 获得
编辑:代码
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.SwitchPreference;
@SuppressWarnings("deprecation")
public class SettingsActivity extends PreferenceActivity {
private static final String tag = "PREFERENCES";
SwitchPreference appPower;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_layout);
setupPreferenceElements();
LogHelper.i(tag, "Preferences Activity Started");
}
private void setupPreferenceElements() {
appPower = (SwitchPreference) getPreferenceManager().findPreference(
"power");
appPower.setChecked(SettingsManager.getPower());
appPower.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
LogHelper.i(tag, "App Power Changed to " + newValue.toString());
SettingsManager.setPower(Boolean.parseBoolean(newValue
.toString()));
return true;
}
});
}
}