android使用sharepreference保存用户操作数据
2014-07-18 12:27
597 查看
很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友。对于软件配置参数的保存,如果是window软件通常我们会采用ini文件进行保存,如果是j2se应用,我们会采用properties属性文件进行保存。如果是Android应用,我们最适合采用什么方式保存软件配置参数呢?Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package
name>/shared_prefs目录下
资源目录图:
![](http://img.blog.csdn.net/20140718123021923)
布局layout代码:
MainActivity.java代码:
PreferencesService.java代码:
布局效果:
![](http://img.blog.csdn.net/20140718123029006)
保存在ddd.xml中的数据:
name>/shared_prefs目录下
资源目录图:
布局layout代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/name" /> <EditText android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/name" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/age" /> <EditText android:id="@+id/age" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/age" /> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/saveBtn" android:onClick="save" /> </LinearLayout>
MainActivity.java代码:
package org.dyb.sharepreferencedemo; import java.util.Map; import org.dyb.service.PreferencesService; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText nameEdit; private EditText ageEdit; PreferencesService service; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameEdit=(EditText) findViewById(R.id.name); ageEdit=(EditText) findViewById(R.id.age); //saveBtn=(Button) findViewById(R.id.save); service=new PreferencesService(this); //回显 Map<String,String> map=service.getPreferences(); nameEdit.setText(map.get("name")); ageEdit.setText(map.get("age")); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void save(View v){ String nameString=nameEdit.getText().toString(); String ageString=ageEdit.getText().toString(); service.save(nameString, Integer.valueOf(ageString)); Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show(); } }
PreferencesService.java代码:
package org.dyb.service; import java.util.HashMap; import java.util.Map; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; public class PreferencesService { private Context context; public PreferencesService(){} public PreferencesService(Context context){ this.context=context; } public void save(String name,int age){ SharedPreferences preferences=context.getSharedPreferences("ddd", Context.MODE_PRIVATE); Editor edit=preferences.edit(); //数据是放在内存中的 edit.putString("name", name); edit.putInt("age", age); //提交方法,把内存中的数据提交到文件中 edit.commit(); } //获取保存的文件内容 public Map<String,String> getPreferences(){ Map<String,String> param=new HashMap<String, String>(); SharedPreferences preferences=context.getSharedPreferences("ddd", Context.MODE_PRIVATE); param.put("name", preferences.getString("name", "")); param.put("age", String.valueOf(preferences.getInt("age", 0))); return param; } }
布局效果:
保存在ddd.xml中的数据:
相关文章推荐
- android使用sharepreference保存用户操作数据
- 【android, 3】3.操作数据保存到rom,sd卡上,sharedpreference的使用,pul解析xml
- Android开发-使用SharedPreferences保存数据-常用操作
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
- 使用GDI+保存用户的绘图数据.
- android---数据保存之SQLite(实现登录用户信息保存)
- android(18)_数据存储与访问_SQLite数据库_使用SQLiteDatabase操作SQLite数据库及事务
- Android之使用ContentResolver对通信录中的数据进行简单操作
- android 中使用ContentResolver操作ContentProvider中的数据
- 在AndroidManifest中使用meta-data保存数据完整示例
- Android游戏开发12:使用SharedPreference与FileInputStream/FileOutputStream保存数据
- Android开发(6)--完成登陆界面的数据保存回显的操作
- Android中使用SharedPreferences进行数据存储及文件操作模式
- 使用NSUserDefaults保存简单的用户数据
- Android使用SharedPreferences保存数据实例
- Android编程心得-使用Application类保存应用的全局数据
- android数据操作 安卓SharedPreferences(保存参数)-作者:逝去=秋
- android中dom操作xml文件(保存和读取数据)
- 在AndroidManifest中使用meta-data保存数据完整示例
- Android 事务操作和数据显示(ListView的使用)