2012-08-25 16 views

回答

0
SharedPreferences settings=getSharedPreferences("mypref", 0); 
et.setText(settings.getString("tvalue"," ")); 

然后重写的onStop()方法

@Override

protected void onStop() { 
    // TODO Auto-generated method stub 
    super.onStop(); 
    SharedPreferences settings=getSharedPreferences("mypref", 0); 
    SharedPreferences.Editor editor=settings.edit(); 
    editor.putString("tvalue", et.getText().toString()); 
    editor.commit(); 

} 
1

其实有3种方式在android系统中留存数据:

  1. 共享偏好
  2. 本地文件(XML,CSV等)
  3. 的Sql数据库精简版

下面是一些代码,以帮助您开始使用“共享偏好”

//get reference to a Shared Preferences instance 
SharedPreferences preferences = context.getSharedPreferences("Some Name You Make Up", 0); 

//add a string to the given Shared Preferences store by key 
SharedPreferences.Editor editor = preferences.edit(); 
editor.putString("some_key", "some_value"); 

//read the value from the Shared preferences 
String value = preferences.getString("some_key", " "); 

这里是链接到共享偏好的Android文档。

http://developer.android.com/reference/android/content/SharedPreferences.html

享受!