Android的数据存储(一)

2010-04-09 15:35

        Android的数据存储有五种方式,SharedPreferences,文件存储,SQLite数据库方式,内容提供器(Content provider)和网络。

       一、SharedPreferences存储

             public class SharedPreferencesActivity extends Activity {
    /** Called when the activity is first created. */
   
    EditText nameET;
    EditText passwordET;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        nameET = (EditText)findViewById(R.id.nameEditText);
        passwordET = (EditText)findViewById(R.id.passwordEditText);
        SharedPreferences settings = getSharedPreferences("SETTING_INFOS", 0);//获取一个SharedPreferences对象
        String name = settings.getString("Name", "");//取出保存的Name
        String password = settings.getString("Password", "");
        nameET.setText(name);
        passwordET.setText(password);
       
    }
   
    protected void onStop(){
    super.onStop();
    SharedPreferences settings = getSharedPreferences("SETTING_INFOS", 0);
    settings.edit()
    .putString("Name", nameET.getText().toString())
    .putString("Password", passwordET.getText().toString())
    .commit();
    //将用户名和密码保存进去
    }
} 

  注:SharedPerferences是以XML的形式保存在DDMS中的File Explorer中展开的/data/data/<package name>/shared_prefs下

你可能感兴趣的:(存储,android,string,sqlite,数据库,class)