今日安卓学习总结:“SharedPrefferences存储技术

安卓数据库存储技术

SharedPrefferences存储技术

1.什么是SharedPrefferences存储

他是Andriod提供的,用来以最简单的方式对数据进行永久保存的方法

2.使用SharedPrefferences存储步骤

  • 获取SharedPrefferences对象
    1. getSharePreferences()

      有两个参数:getSharePreferences(String name,int mode)

      • name:SharePreferences文件名

      • mode:用于指定访问权限:MODE_PRIVATE(被本应用读写)

        /MODE_MULTIPROCESS(跨应用读写)

    2. getPrefences()

      只有一个参数: getPreferences(int mode)同样也是用于指定访问权限

      • MODE_PRIVATE

        (被本应用读写)

      • MODE_MULTI_PROCESS

        (跨应用读写)

  • 获得SharedPrefferences.Editor对象

    调用edit()方法实现

  • 向SharedPrefferences.Editor对象中添加数据

    put + 数据类型

    1. putBoolean()
    2. putString
    3. putInt()
  • 提交数据

commit()

3.使用SharedPrefferences读取数据步骤

  • 获取SharedPreference对象(getSharePreferences()/getPerferences())

  • 使用SharedPrefenrences类提供的getXXX()方法读取数据

    getBoolean() ,getString(), getInt()

4.实例:

模拟手机QQ自动登录的功能,实现使用SharedPreferences存储保存输入的账号和密码

开发步骤:

  1. 布局界面
  2. 获取SharedPreferences对象
  3. 实现自动登录功能
  4. 实现手动登录并存储账号与密码功能

页面布局;

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/qq1"
        android:layout_centerHorizontal="true"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:src="@drawable/qq"/>
    <EditText
        android:id="@+id/username"
        android:layout_width="300dp"
        android:layout_height="60dp"
        android:layout_below="@id/qq1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp"
        android:background="@drawable/editst"
        android:hint="用户名"
        android:gravity="center"/>
    <EditText
        android:background="@drawable/editst"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/username"
        android:id="@+id/password"
        android:hint="密码"
        android:inputType="textPassword"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:layout_width="300dp"
        android:layout_height="60dp"/>
    <LinearLayout
        android:id="@+id/line1"
        android:layout_below="@id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <RadioButton
            android:layout_marginTop="10dp"
            android:layout_marginLeft="44dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="已阅读并同意服务协议和qq隐私保护指引"/>
    </LinearLayout>

    <Button
        android:id="@+id/register"
        android:layout_width="300dp"
        android:layout_height="60dp"
        android:layout_below="@+id/line1"
        android:layout_centerHorizontal="true"
        android:text="登录" />
    <TextView
        android:layout_marginLeft="300dp"
        android:layout_marginBottom="15dp"
        android:id="@+id/hao"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册账号"/>

    <TextView
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="15dp"
        android:id="@+id/mi"
      android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="找回密码"/>



</RelativeLayout>

mainActivity;

package com.example.qqregister;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private String mr ="lucky",mrsoft="123456";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText usernameET =(EditText) findViewById(R.id.username);
        final EditText passwordET =(EditText) findViewById(R.id.password);
        Button login = (Button) findViewById(R.id.register);
       //获取Shared Preferences对象
        final SharedPreferences sp = getSharedPreferences("mrsoft",MODE_PRIVATE);
        //实现自动登录功能
        String username =sp.getString("username",null);//获取账号信息
        String password  =sp.getString("password",null);//获取密码
        if(username !=null && password !=null){
               if(username.equals(mr)&&password.equals(mrsoft)){
                   Intent intent = (Intent) new Intent(MainActivity.this, afterRegister.class);
                   startActivity(intent);//跳转activity
               }
        }else{
            //实现手动登录,并且存储账号密码
          login.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  //获取输入的账号
                  String in_username=usernameET.getText().toString();
                  //获得输入密码
                  String in_password=passwordET.getText().toString();
                  SharedPreferences.Editor editor = sp.edit();//获取editor对象
                  if(in_username.equals(mr) &&in_password.equals(mrsoft)){
                      editor.putString("username",in_username);//保存账号
                      editor.putString("password",in_password);//保存密码
                      editor.commit();//提交信息
                        //实现跳转页面操作
                      Intent intent = (Intent) new Intent(MainActivity.this, afterRegister.class);
                      startActivity(intent);//跳转activity
                      Toast.makeText(MainActivity.this, "已保存账号和密码", Toast.LENGTH_SHORT).show();
                  }else{
                      Toast.makeText(MainActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                  }
              }
          });
        }


    }
}

实例运行图:
在这里插入图片描述
在这里插入图片描述
今日安卓学习到此结束,还有下期!我会持续更新!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值