java源文件:
package com.lp;
import android.app.Activity;import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
public class TestSharePrefereActivity extends Activity {
private SharedPreferences sp=null;
private EditText editText;
private final String SPF_KEY = "spf";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取当前sp的内容初始化给EditText
sp = getPreferences(MODE_WORLD_WRITEABLE );editText = (EditText) findViewById(R.id.edit);
String rs = sp.getString(SPF_KEY, null);
if (rs != null) {
editText.setText(rs);
}
}
@Override
protected void onDestroy(){
SharedPreferences.Editor spEdit = sp.edit();
//退出时保存/修改当前EditText的内容到sp
spEdit.putString(SPF_KEY, String.valueOf(editText.getText()));spEdit.commit();
super.onDestroy();
}
}
main.xml:
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>