Android之使用SharedPreferences保存用户偏好参数
2014-07-04 23:47
489 查看
在Android应用中,我们常需要记录用户设置的一些偏好参数,,此时我们就需要用SharedPreferences和Editor将这些信息保存下来,在下次登录时读取。
SharedPreferences保存的数据主要类似于配置信息格式的数据,因此它保存数据的形式为key-value对,下面我们来看下实例代码。
首先是界面布局,比较简单,就是一个普通的登陆界面.
这是自定义的Preferences 类,用来实现数据的保存 ,可在Android的内置存储空间产生一文件。
下面是Mainactivity的代码。在activity的oncreate阶段我们加载本地的数据。
我们看一下效果.
![](http://images.cnitblog.com/i/624647/201407/042334395741253.png)
点击保存参数,出现保存完成则说明我们已经保存成功了,在下次登录的时候可以看到这些参数还在。因为记录文件是在内置空间中的,所以我们在SD卡中找不到该文件,
如果有root权限的手机可以下载个RE文件管理,我们可以再/data/data/的路径找到很多应用程序的内置文件夹,我们可以在这些文件夹中看到一个shared_prefs文件夹,
里面就有我们刚刚设置而产生的xml文件。
![](http://images.cnitblog.com/i/624647/201407/042345072625049.png)
SharedPreferences保存的数据主要类似于配置信息格式的数据,因此它保存数据的形式为key-value对,下面我们来看下实例代码。
首先是界面布局,比较简单,就是一个普通的登陆界面.
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/account" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/password" android:layout_below="@id/account" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/password" android:text="保存参数" android:id="@+id/save" android:onClick="save" /> </RelativeLayout>
这是自定义的Preferences 类,用来实现数据的保存 ,可在Android的内置存储空间产生一文件。
import android.R.integer; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.widget.EditText; public class Preferences { private Context context; public Preferences(Context context) { this.context=context; } public void save(String name, Integer valueOf) { //保存文件名字为"shared",保存形式为Context.MODE_PRIVATE即该数据只能被本应用读取 SharedPreferences preferences=context.getSharedPreferences("shared",Context.MODE_PRIVATE); Editor editor=preferences.edit(); editor.putString("name", name); editor.putInt("age", valueOf); editor.commit();//提交数据 } }
下面是Mainactivity的代码。在activity的oncreate阶段我们加载本地的数据。
import java.util.HashMap; import java.util.Map; import android.R.integer; import android.os.Bundle; import android.app.Activity; import android.content.SharedPreferences; 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 account,passworad; Preferences prefer;//自定义的类 SharedPreferences preference; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); account=(EditText)findViewById(R.id.account); passworad=(EditText)findViewById(R.id.password); //获取本地的数据 preference=getSharedPreferences("shared", MODE_PRIVATE); Map<String, String> map=new HashMap<String, String>(); map.put("name",preference.getString("name","")); map.put("age", String.valueOf(preference.getInt("age", 0))); account.setText(map.get("name")); passworad.setText(map.get("age")); } //保存文件的方法 public void save(View v) { String name=account.getText().toString(); String age=passworad.getText().toString(); prefer=new Preferences(this); prefer.save(name,Integer.valueOf(age)); Toast.makeText(getApplicationContext(), "保存完成", Toast.LENGTH_SHORT).show(); } @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; } }
我们看一下效果.
![](http://images.cnitblog.com/i/624647/201407/042334395741253.png)
点击保存参数,出现保存完成则说明我们已经保存成功了,在下次登录的时候可以看到这些参数还在。因为记录文件是在内置空间中的,所以我们在SD卡中找不到该文件,
如果有root权限的手机可以下载个RE文件管理,我们可以再/data/data/的路径找到很多应用程序的内置文件夹,我们可以在这些文件夹中看到一个shared_prefs文件夹,
里面就有我们刚刚设置而产生的xml文件。
![](http://images.cnitblog.com/i/624647/201407/042345072625049.png)
![](http://images.cnitblog.com/i/624647/201407/042345200272515.png)
相关文章推荐
- Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数
- android 使用sharedPreferences保存用户设置的参数
- 保存、方法-Android文件存储--采用SharedPreferences保存用户偏好设置参数和读取设置参数-by小雨
- android第十三步采用SharedPreference保存用户偏好设置参数
- Android开发入门之采用SharedPreferences保存用户偏好设置参数
- Android采用SharedPreferences保存用户偏好设置参数
- Android心得4.2--SharedPreference保存用户偏好参数设置.doc
- Android基础入门教程——6.2 数据存储与访问之——SharedPreferences保存用户偏好参数
- Android采用SharedPreferences保存用户偏好设置参数
- Android 使用 SharedPreferences 保存和加载软件参数
- Android 保存用户偏好设置
- 使用SharedPreferences 保存用户偏好
- 16_采用SharedPreferences保存用户偏好设置参数
- Android - 保存用户首次使用状态(SharedPreferences)
- Android 保存用户偏好设置
- saveInstallState参数使用详解(android activity状态保存和恢复)
- 实例教程五:采用SharedPreferences保存用户偏好设置参数
- Android数据存储方式(二)SharedPreferences(偏好参数保存)
- 使用SharedPreferences保存偏好参数
- Android应用中为用户保存偏好设置(传智播客视频笔记)