SharedPreferences是android轻量级存储数据的工具,具体用法如下示例:

public class SharedPreferenceTestActivity extends ActionBarActivity {
private TextView showDataText=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_preference_test);
showDataText=(TextView)findViewById(R.id.show_user_data);
}

//显示数据

public void showUserInfo(View view){
SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
String str=sp.getInt("userId",-1)+" "+sp.getString("name","***")+" "+
sp.getInt("age",-1)+" "+sp.getFloat("money",0);
showDataText.setText(str);
}

//保存数据
public void saveUserInfo(View view){
SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editorSp = sp.edit();
editorSp.putInt("userId", 100);
editorSp.putString("name", "xiaoli");
editorSp.putInt("age", 25);
editorSp.putFloat("money", 800);
editorSp.commit();
}

//清除数据
public void clearUserInfo(View view){
SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.commit();
}

//更新数据
public void updateUserInfo(View view){
SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editorSp = sp.edit();
editorSp.putInt("age", 39);
editorSp.putFloat("money", 1800);
editorSp.commit();
}
}

布局文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context="liyang.com.myapptest.SharedPreferenceTestActivity">
<TextView
android:id="@+id/show_user_data"
android:gravity="center"
android:text="数据显示"
android:textColor="#000000"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:onClick="showUserInfo"
android:text="显示用户信息"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="saveUserInfo"
android:text="保存用户信息"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="updateUserInfo"
android:text="更新用户信息"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="clearUserInfo"
android:text="清楚用户信息"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


更多相关文章

  1. Android - Intent - 传递数据
  2. android webview web里面的数据透传到java以及java的数据透传到w
  3. Android和Java本地数据库新选择
  4. 在Android中查看和管理sqlite数据库
  5. Android图表控件MPAndroidChart——曲线图LineChart(多条曲线)动态
  6. Android 自定义View及其在布局文件中的使用示例(三):结合Android
  7. Android联系人数据库全解析(1)
  8. [转」android中的数据库操作
  9. 数据存储和界面展示一

随机推荐

  1. Android布局文件的加载过程分析:Activity
  2. Android(安卓)访问WebService
  3. Android(安卓)仿百度网页音乐播放器圆形
  4. android SQLite 优化(三)使用事务 优化 ins
  5. Android(安卓)面试题集 包含答案
  6. eclipse编译cocos2d-x2.2.6的Android
  7. Android(安卓)Studio导入项目app module
  8. Android动画介绍汇总
  9. 黑莓学习笔记之二--布局管理器Manager
  10. Android实现侧滑菜单DrawerLayout