Java代碼
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲取一個SharedPreferences對象
SharedPreferences sp = getSharedPreferences("dataInfo", 0);
String user = sp.getString("user", "");
String password = sp.getString("password", "");
((EditText)findViewById(R.id.user)).setText(user);
((EditText)findViewById(R.id.password)).setText(password);
}
@Override
protected void onStop() {
super.onStop();
SharedPreferences sp = getSharedPreferences("dataInfo", 0);
sp.edit().putString("user",
((EditText)findViewById(R.id.user)).getText().toString())
.putString("password",
((EditText)findViewById(R.id.password)).getText().toString())
.commit();
}
作者“rayln”