Android 数据存储
2010-04-10 04:46:00 来源:WEB开发网15.
16. @Override
17. public void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.main);
20.
21. //Find VIew
22. field_name = (EditText) findViewById(R.id.name); //首先获取用来输入用户名的组件
23. filed_pass = (EditText) findViewById(R.id.password); //同时也需要获取输入密码的组件
24.
25. // Restore preferences
26. SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0); //获取一个 SharedPreferences对象
27. String name = settings.getString(NAME, ""); //取出保存的 NAME
28. String password = settings.getString(PASSWORD, ""); //取出保存的 PASSWORD
29.
30. //Set value
31. field_name.setText(name); //将取出来的用户名赋予field_name
32. filed_pass.setText(password); //将取出来的密码赋予filed_pass
33. }
34.
35. @Override
36. protected void onStop(){
37. super.onStop();
38.
39. SharedPreferences settings = getSharedPreferences(SETTING_INFOS, 0); //首先获取一个 SharedPreferences对象
40. settings.edit()
41. .putString(NAME, field_name.getText().toString())
42. .putString(PASSWORD, filed_pass.getText().toString())
43. .commit();
44. } //将用户名和密码保存进去
45.
46. }
package com.demo; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.widget.EditText; public class SharedPreferencesDemo extends Activity { public static final String SETTING_INFOS = "SETTING_Infos"; public static final String NAME = "NAME"; public static final String PASSWORD = "PASSWORD"; private EditText field_name; //接收用户名的组件 private EditText filed_pass; //接收密码的组件 @Override
更多精彩
赞助商链接