我想我在从SharedPreferences
.
我的代码:
public class SecondActivity extends Activity {
private TextView password;
private EditText username;
private Button button;
private String name;
private String pass;
private Set<String> set;
private TextView show;
SharedPreferences myPrefs;
SharedPreferences.Editor editor;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
myPrefs = this.getPreferences(MODE_PRIVATE);
editor = myPrefs.edit();
username = (EditText) findViewById(R.id.editText1);
password = (EditText) findViewById(R.id.editText2);
button = (Button) findViewById(R.id.button1);
show = (TextView) findViewById(R.id.textView3);
name = username.getText().toString();
pass = password.getText().toString();
/*
* set = new HashSet<String>(); set.add(name); set.add(pass);
*/
}
public void saveToPreference(View v) {
editor.putString("UserName", name);
editor.putString("Password", pass);
editor.commit();
SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String use = myPref.getString("UserName", "Sorry");
String pas = myPref.getString("Password", "SorryAgain");
Toast.makeText(getApplicationContext(), use, Toast.LENGTH_SHORT).show();
show.append(use + "\n" + pas);
}
}
应用程序不会强制/关闭。相反,我得到了 defaultValues;"Sorry","SorryAgain" 检索自SharedPreferences
。"Sorry"
被烤了。
我还应该做什么?