我一直在嘗試以下方法來獲取對SharedPreference所做的任何更改的textview更新,但我很難搞清楚我的下一步行動。任何人? :)如何在SharedPreference更改時更新TextView?
public class CallForwardActivity extends Activity implements
OnSharedPreferenceChangeListener {
SharedPreferences userInfo;
Context context;
UserController userControl;
private static final String USERINFO_FILE = "StoredUserInfo";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.callforward);
context = this;
userInfo = this.getSharedPreferences(USERINFO_FILE, 0);
userControl = new UserController(context);
final TextView setActiveNumberTV = (TextView)
findViewById(R.id.activeNumberTextView);
setActiveNumberTV.setText(userControl.GetActiveNumber());
final RelativeLayout showAvailablenumbers = (RelativeLayout)
findViewById(R.id.showAvailableNumbers);
showAvailablenumbers.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getParent(),
AvailableNumbersActivity.class);
SettingsActivityGroup parentActivity =
(SettingsActivityGroup) getParent();
parentActivity.ReplaceView(
"Show Available Numbers Activity", i);
}
});
}
public void onSharedPreferenceChanged(SharedPreferences arg0,
String arg1) {
final TextView setActiveNumberTV = (TextView)
findViewById(R.id.activeNumberTextView);
setActiveNumberTV.setText("...");
}
}
你錯過了註冊onSharedPreferenceChnaged()..! – user370305 2012-02-22 12:55:22