我正在处理 listpreferences 并且我已经实现了 OnSharedPreferenceChangeListener 以根据在 listpreference 上选择的项目来更改 textview 上的文本。但是,它没有注册任何更改,并且我已经没有想法了,因此将不胜感激。谢谢
public class pref extends PreferenceActivity implements OnSharedPreferenceChangeListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
setContentView(R.layout.main);
TextView text = (TextView) findViewById(R.id.textView1);
String keys = null;
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.registerOnSharedPreferenceChangeListener(this);
keys = pref.getString("listPref1", "");
if (keys == "ON")
{
text.setText("ON");
}
else if (keys == "OFF")
{
text.setText("OFF");
}
else{
text.setText("Choose One");
}
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
//What code to I put in here to have the listener register the change
}
}