Android之使用SharedPreferences保存用户偏好参数
Android之使用SharedPreferences保存用户偏好参数
https://www.cnblogs.com/zhrxidian/p/3820507.html
在Android应用中,我们常需要记录用户设置的一些偏好参数,,此时我们就需要用SharedPreferences和Editor将这些信息保存下来,在下次登录时读取。
SharedPreferences保存的数据主要类似于配置信息格式的数据,因此它保存数据的形式为key-value对,下面我们来看下实例代码。
首先是界面布局,比较简单,就是一个普通的登陆界面.
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity" > 10 <EditText 11 android:layout_width="fill_parent" 12 android:layout_height="wrap_content" 13 android:id="@+id/account" 14 /> 15 <EditText 16 android:layout_width="fill_parent" 17 android:layout_height="wrap_content" 18 android:id="@+id/password" 19 android:layout_below="@id/account" 20 /> 21 <Button 22 android:layout_width="fill_parent" 23 android:layout_height="wrap_content" 24 android:layout_below="@id/password" 25 android:text="保存参数" 26 android:id="@+id/save" 27 android:onClick="save" 28 /> 29 </RelativeLayout>
这是自定义的Preferences 类,用来实现数据的保存 ,可在Android的内置存储空间产生一文件。
1 import android.R.integer;
2 import android.content.Context;
3 import android.content.SharedPreferences;
4 import android.content.SharedPreferences.Editor;
5 import android.widget.EditText;
6
7 public class Preferences {
8
9 private Context context;
10 public Preferences(Context context)
11 {
12 this.context=context;
13 }
14
15
16 public void save(String name, Integer valueOf)
17 {
18 //保存文件名字为"shared",保存形式为Context.MODE_PRIVATE即该数据只能被本应用读取
19 SharedPreferences preferences=context.getSharedPreferences("shared",Context.MODE_PRIVATE);
20
21 Editor editor=preferences.edit();
22 editor.putString("name", name);
23 editor.putInt("age", valueOf);
24
25 editor.commit();//提交数据
26 }
27
28
29 }
下面是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;
}
}
我们看一下效果.

点击保存参数,出现保存完成则说明我们已经保存成功了,在下次登录的时候可以看到这些参数还在。因为记录文件是在内置空间中的,所以我们在SD卡中找不到该文件,
如果有root权限的手机可以下载个RE文件管理,我们可以再/data/data/的路径找到很多应用程序的内置文件夹,我们可以在这些文件夹中看到一个shared_prefs文件夹,
里面就有我们刚刚设置而产生的xml文件。






浙公网安备 33010602011771号