Android存储(一)——SharedPreferences

本文介绍如何使用Android的SharedPreferences来保存简单的登录信息,包括用户名和密码,并通过一个示例展示了如何实现记住密码的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SharePreferences作为android存储方式的一种,常用于保存应用的一些配置信息或笔较简短的数据。下面以记住密码为例来介绍。

以下是主要的写入和读取函数。

一、写入SharedPreferences

/*写入到SharePreferences函数
    @parmer name 用户名
    @parmer pwd 密码
*/
private void writeSharePreference(String name,String pwd){
        SharedPreferences p = getSharedPreferences("userinfo",MODE_PRIVATE);//获取SharePreferences对象
        SharedPreferences.Editor editor = p.edit();
        //获取编辑对象Editor

        //保存数据的键值对
        editor.putString("username",name);
        editor.putString("password",pwd);
        //提交给系统
        editor.commit();
    }

二、读取SharePreferences数据

//为了方便处理,可以先定义一个内部类来存放数据
private class UserInfo{
    public String userName;//用户名
    public String password;//密码
}


/*读取SharePrefences的内容
    @return info 返回UserInfo
*/
private UserInfo readSharePerences(){
        UserInfo info = new UserInfo();
        SharedPreferences p = getSharedPreferences("userinfo",MODE_PRIVATE);
        //getString读取对应的字段,第一个是字段名,第二个是没有的默认值
        info.userName = p.getString("username","");
        info.password = p.getString("password","");
        return info;
    }

以下是利用SharePrefencess实现的登陆界面记住密码的带码。
先看看效果
SharePrefences示例图片

界面代码:

//activity_main.xml
<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:text="姓名:"
            />
        <EditText
            android:id="@+id/edt_username"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:hint="输入姓名"
            />
        </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_height="wrap_content"
            android:layout_width="100dp"
            android:text="密码:"
            />
        <EditText
            android:id="@+id/edt_password"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:password="true"
            android:hint="输入密码"
            />
    </LinearLayout>
    <CheckBox
        android:id="@+id/ckb_remember"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:layout_marginLeft="50dp"
        />
    <Button
        android:id="@+id/btn_login"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="登陆"
        />
</LinearLayout>

主要代码:

//MainActivity代码
package com.example.antent.sharedpreferencesde;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText mUsername;
    private EditText mPassword;
    private CheckBox mCheckBox;
    private Button mLogin;
    private UserInfo info = new UserInfo();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        firstIn();

    }

    /*
        初次进入软件时执行读取
    */
    private void firstIn(){
        info = readSharePerences();
        mUsername.setText(info.userName);
        mPassword.setText(info.password);
        mCheckBox.setChecked(true);
    }
/*
    界面初始化
*/
    private void initView(){
        mUsername = (EditText)findViewById(R.id.edt_username);
        mPassword = (EditText)findViewById(R.id.edt_password);
        mCheckBox = (CheckBox)findViewById(R.id.ckb_remember);
        mLogin = (Button)findViewById(R.id.btn_login);
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                info.userName = mUsername.getText().toString();
                info.password = mPassword.getText().toString();
                if (mCheckBox.isChecked()) {
                    writeSharePreference(info);
                    Toast.makeText(MainActivity.this, "保存账号密码成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "不保存账号密码", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void writeSharePreference(String name,String pwd){
        SharedPreferences p = getSharedPreferences("userinfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = p.edit();
        editor.putString("username",name);
        editor.putString("password",pwd);
        editor.commit();
    }
/*

    用UserInfo对象写入配置
*/
    private void writeSharePreference(UserInfo info){
        SharedPreferences p = getSharedPreferences("userinfo",MODE_PRIVATE);
        SharedPreferences.Editor editor = p.edit();
        editor.putString("username",info.userName);
        editor.putString("password",info.password);
        editor.commit();
    }
/*

    读取配置
*/

    private UserInfo readSharePerences(){
        UserInfo info = new UserInfo();
        SharedPreferences p = getSharedPreferences("userinfo",MODE_PRIVATE);
        if(p!=null) {
            info.userName = p.getString("username", "");
            info.password = p.getString("password", "");
        }
        return info;
    }
    private class UserInfo{
        public String userName="";
        public String password="";
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值