虽然比较简单,但这个能略微做到通用,后期会继续优化,做好一件事,比做完一件事要麻烦多了。
package com.king.android.db;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import com.king.android.R;
/**
* 描述:使用SharePreference存储
* 作者:Andy.Liu
* 时间: 2012-7-9 上午07:51:22
**/
public class SharePreferenceActivity extends Activity implements OnClickListener{
/**保存字符串的key*/
public static final String STRING_KEY ="string_key";
/**保存字符串的的名字*/
public static final String STRING_NAME ="string_name";
Context mContext = null;
EditText edSave,edRead;
Button btnSave,btnRead;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = SharePreferenceActivity.this;
initView();
}
private void initView(){
setContentView(R.layout.store_layout);
edSave = (EditText) findViewById(R.id.ed_edit);
edRead = (EditText) findViewById(R.id.ed_show);
btnSave = (Button) findViewById(R.id.btn_save);
btnSave.setOnClickListener(this);
btnRead = (Button) findViewById(R.id.btn_read);
btnRead.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_save:
SharePreferenceTools.doStore(mContext,STRING_NAME,STRING_KEY,edSave.getText().toString());
break;
case R.id.btn_read:
String showText = SharePreferenceTools.doGet(mContext,STRING_NAME,STRING_KEY);
edRead.setText(showText);
break;
}
}
final static class SharePreferenceTools{
/**
*
*TODO:保存数据
*Author:Andy.Liu
*Create Time:2012-7-9 上午08:38:51
*TAG:@param context
*TAG:@param strName 保存的文件名
*TAG:@param key 保存字符串的key
*TAG:@param str 字符串的内容
*Return:void
*/
public static void doStore(Context context,String strName,String key,String str){
SharedPreferences share = context.getSharedPreferences(strName, Context.MODE_PRIVATE);
Editor editor = share.edit();
editor.putString(key, str);
editor.commit();
}
/**
*
*TODO:获取保存的字符串
*Author:Andy.Liu
*Create Time:2012-7-9 上午08:41:35
*TAG:@param contxt
*TAG:@param strName 字符串文件名
*TAG:@param key 字符串key
*TAG:@return
*Return:String
*/
public static String doGet(Context context,String strName,String key){
SharedPreferences share = context.getSharedPreferences(strName, Context.MODE_WORLD_WRITEABLE|Context.MODE_WORLD_READABLE);
return share.getString(key, "");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/ed_edit"
android:gravity="left|top"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:hint="请输入要保存的内容"/>
<Button
android:id="@+id/btn_save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="保存" />
<EditText
android:id="@+id/ed_show"
android:gravity="left|top"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:hint="展示保存的内容"/>
<Button
android:id="@+id/btn_read"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="读取保存内容" />
</LinearLayout>