在android中保存数据一般有sqlite数据库,文件(File),SharedPreferences常见的就这几种,像什么存储在网络几乎不用,今天就讲讲SharedPreferences怎么保存数据的,SharedPreferences用起来很简单,想当于map集合,以键值对的形式存储在文件中,一般的应用常见:像登陆记住密码的功能,今天就以这个例子来说说SharedPreferences
现在建一个android工程,login,
布局文件:
<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="wrap_content"
android:orientation="vertical"
>
<EditText
android:id="@+id/et_username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="用户名" />
<EditText
android:id="@+id/et_pwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="密码" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
>
<Button
android:id="@+id/login"
android:layout_width="90dp"
android:layout_height="40dp"
android:text="登陆"
android:layout_alignParentRight="true"
/>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="记住密码"
android:layout_alignBaseline="@id/login"
/>
</RelativeLayout>
</LinearLayout>
这是写demo,如果做项目一些字符串肯定是要放在value/string.xml文件中,
当勾选复选框的时候,当退出应用再次进来的时候,用户名和密码就自动显示
MainActivity.java
public class MainActivity extends Activity {
private EditText et_username;
private EditText et_pwd;
private Button login;
private CheckBox check;
private String username;
private String pwd;
private SharedPreferences sp;
private Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
sp = getSharedPreferences("config", Context.MODE_PRIVATE);
editor = sp.edit();
boolean isSave = sp.getBoolean("isCheck", false);
if(isSave){
et_username.setText(sp.getString("username", ""));
et_pwd.setText(sp.getString("pwd", ""));
check.setChecked(true);
}
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
username = et_username.getText().toString();
pwd = et_pwd.getText().toString();
boolean isCheck = check.isChecked();
editor.putString("username", username);
editor.putString("pwd", pwd);
editor.putBoolean("isCheck", isCheck);
editor.commit();
}
});
}
/**
* 初始化控件
*/
private void initView() {
et_username = (EditText) findViewById(R.id.et_username);
et_pwd = (EditText) findViewById(R.id.et_pwd);
login = (Button) findViewById(R.id.login);
check = (CheckBox) findViewById(R.id.check);
}
}
一般布局文件中控件很多时候,在findViewById的时候最好封装在一个方法中,这样增强易读性,如果是在做实际的项目中,像SharedPreferences最好封装在一个类中,因为做项目肯定不知你一个人使用,还有你的同事,而且封装的类最好用单例模式,不让每次都去创建其对象,因为可能使用的地方比较多,
SharedPreferencesUtil.java封装了SharedPreferences的一些操作,
package com.example.login.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
public class SharedPreferencesUtil {
private static SharedPreferencesUtil instance ;
private static SharedPreferences sp;
private static Editor editor;
private SharedPreferencesUtil(){
}
public static synchronized SharedPreferencesUtil getInstance(Context context){
if(instance==null){
Log.e("SharedPreferencesUtil","第一次走这里");
instance = new SharedPreferencesUtil();
sp = context.getSharedPreferences("user", Context.MODE_PRIVATE);
editor = sp.edit();
}
Log.e("SharedPreferencesUtil","第二次走这里");
return instance;
}
public static void putString(String key,String value){
editor.putString(key, value);
editor.commit();
}
public static String getString(String key){
return sp.getString(key, "");
}
public static void putBoolean(String key,boolean value){
editor.putBoolean(key, value);
editor.commit();
}
public static boolean getBoolean(String key){
return sp.getBoolean(key, false);
}
}
568

被折叠的 条评论
为什么被折叠?



