您的位置:首页 > 移动开发 > Android开发

Android学习笔记-SharedPreferences

2016-03-20 17:54 656 查看
SharedPreences是Android平台上面一个轻量级的存储类,主要存储一些应用程序的配置参数,例如用户名。密码。自定义参数的设置。这里要注意下面代码中的两点:

注意事项

@null代表背景没有颜色

context是引用上下文对象,需要通过context.getSharedPreferences实例化这个对象

xml文件放在data/data/<项目文件>/shared_pres里面

下面是界面布局代码

<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="com.example.pc_bbj.study_sharedpreferences.MainActivity">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/iv_head"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
android:src="@mipmap/dog"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layout"
android:layout_below="@id/iv_head"
android:layout_margin="10dp"
android:background="#ffffff"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rl_username"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_name"
android:layout_centerVertical="true"
android:text="账号"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_number"
android:layout_toRightOf="@id/tv_name"
android:background="@null"
android:layout_marginLeft="5dp"
/>
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#E6E6E6"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rl_userpsw"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_psw"
android:layout_centerVertical="true"
android:text="密码"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_password"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/tv_psw"
android:inputType="textPassword"
android:background="@null"/>
</RelativeLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn_login"
android:layout_below="@+id/layout"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="#3C8DC4"
android:text="登录"
android:textColor="#ffffff"/>

</RelativeLayout>






下面是这个工具类

package com.example.pc_bbj.study_sharedpreferences;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

/**
* Created by PC_BBJ on 2016/3/20.
*/
public class Utils {
//保存QQ号号码和登录密码到data.xml文件中
public static boolean saveUserInfo(Context context,String number,String password){
SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
SharedPreferences.Editor edit=sp.edit();//获取Editor对象来修改这个存储类
edit.putString("userName",number);
edit.putString("pwd", password);
edit.commit();//上传同步数据
return true;
}
//从data.xml文件中获取存储QQ号码和密码
public static Map<String,String> getUserInfo(Context context){
SharedPreferences sp=context.getSharedPreferences("data", Context.MODE_PRIVATE);
String number=sp.getString("userName", null);
String password=sp.getString("password",null);//null是defaultValue
Map<String,String> userMap=new HashMap<String,String>();
userMap.put("number",number);
userMap.put("password",password);
return userMap;
}
}


下面是MainActivity类

package com.example.pc_bbj.study_sharedpreferences;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

/*
SharedPreferences是android平台上面一个轻量级的存储类,主要用户存储一些应用程序的配置参数,例如用户名和密码
SharedPreferences是以键值对的形式保存在xml文件中
context.getSharedPreferences获取SharedPreferences获取实例对象
* */

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

private EditText etNumber;
private EditText etPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//取出号码
Map<String,String> userInfo=Utils.getUserInfo(this);//传入的是Context上下文对象
if(userInfo!=null){
//显示在界面上
etNumber.setText(userInfo.get("number"));
etNumber.setText(userInfo.get("password"));
}
}

private void initView() {
etNumber=(EditText)findViewById(R.id.et_number);
etPassword=(EditText)findViewById(R.id.et_password);
findViewById(R.id.btn_login).setOnClickListener(this);
}

@Override
public void onClick(View v) {
//当单机QQ号和密码时,获取Q号和密码
String number=etNumber.getText().toString().trim();
String password=etPassword.getText().toString().trim();
//检验号码和密码是否正确
if(TextUtils.isEmpty(number)){
Toast.makeText(this,"请输入Dog号码",Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(this,"请输入密码",Toast.LENGTH_SHORT).show();
return;
}
//登录成功
Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
//如果正确,判断是否够炫了记住密码
Log.i("MainActivity","记住密码:"+number+","+password);
//保存用户信息
boolean isSaveSuccess=Utils.saveUserInfo(this,number,password);
if(isSaveSuccess){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: