SharedPreferences
来源:互联网 发布:孤独探戈知乎 编辑:程序博客网 时间:2024/06/18 15:25
SharedPreferences的本质是基于XML文件存储key-value键值对数据。
使用步骤:
1、得到SharedPreferences对象
2、调用SharedPreferences对象的edit()方法来获取一个SharedPreferences.Editor对象。
3、向SharedPreferences.Editor对象中添加数据。
4、调用commit方法将添加的数据提交。
下面实现一个小程序来说明其用法:
这里写代码片package cn.bzu.edu.login;import android.content.DialogInterface;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;Main_activity:import cn.bzu.edu.login.cn.itcast.saveqq.utils.Utils;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); if (userInfo!=null){ etNumber.setText(userInfo.get("number")); etPassword.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); } public void onClick(View v){ String number=etNumber.getText().toString().trim(); String password=etPassword.getText().toString().trim(); if (TextUtils.isEmpty(number)){ Toast.makeText(this,"请输入QQ号码",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(); } }}
这里写代码片utils:package cn.bzu.edu.login.cn.itcast.saveqq.utils;import android.content.Context;import android.content.SharedPreferences;import java.util.HashMap;import java.util.Map;/** * Created by Administrator on 2017/6/16. */public class Utils { public static boolean saveUserInfo(Context context,String number,String password){ SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE); SharedPreferences.Editor editor=sp.edit(); editor.putString("username",number); editor.putString("pwd",password); editor.commit(); return true; } 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("pwd",null); Map<String,String> userMap=new HashMap<String, String>(); userMap.put("number",number); userMap.put("password",password); return userMap; }}
这里写代码片activity_main:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e6e6e6" android:orientation="vertical" tools:context="cn.bzu.edu.login.MainActivity"> <ImageView android:id="@+id/iv_head" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_centerHorizontal="true" android:layout_marginTop="40dp" android:src="@drawable/qiqiqi"/> <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/iv_head" android:layout_margin="10dp" android:background="#ffffff" android:orientation="vertical"> <RelativeLayout android:id="@+id/r1_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="账号"/> <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/tv_name" android:background="@null"/> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#e6e6e6"/> <RelativeLayout android:id="@+id/r1_userpsw" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <TextView android:id="@+id/tv_psw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="密码"/> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/tv_psw" android:inputType="textPassword" android:background="@null"/> </RelativeLayout> </LinearLayout> <Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/layout" android:layout_centerHorizontal="true" android:layout_marginLeft="10dp" android:layout_marginTop="20dp" android:background="#3c8dc4" android:text="登录" android:textColor="#ffffff"/></LinearLayout>
阅读全文
0 0
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- SharedPreferences
- sharedPreferences
- easyui 设置dataGrid选中行的背景颜色
- 深入理解 Android 卷I
- C++知识点-虚函数的作用和使用方法
- 数据结构基础温故-3.队列
- 艺多不压身
- SharedPreferences
- win10-ubuntu安装screenfetch
- charAt的用法
- python pandas学习
- B2Ctt商城02
- tensorflow GPU环境配置
- 芯片测试
- 案例JDBC 基本操作(二)
- 二叉树——AVL