SharedPreferences

1.SharedPreferences是什么

SharedPreferences是一种轻量级的数据存储方式,用来保存应用的一些常用配置,它是Android数据持久化方法中最简单的一种。

2.SharedPreferences如何存储数据,如何读取数据

1、根据Context获取SharedPreferences对象
2、利用edit()方法获取Editor对象。
3、通过Editor对象存储key-value键值对数据。
4、通过commit()方法提交数据。

使用SharedPreferences储存存数据方法如下:

//实例化SharedPreferences对象(第一步) 
SharedPreferences mySharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE); 

//实例化SharedPreferences.Editor对象(第二步) 
SharedPreferences.Editor editor = mySharedPreferences.edit(); 

//用putString的方法保存数据 
editor.putString("name", "Karl"); 
editor.putString("habit", "sleep"); 

//提交当前数据 
editor.commit(); 

//使用toast信息提示框提示成功写入数据 
Toast.makeText(this, "数据成功写入SharedPreferences!" , Toast.LENGTH_LONG).show();

使用SharedPreferences读取数据方法如下:

/同样,在读取SharedPreferences数据前要实例化出一个SharedPreferences对象 
SharedPreferencessharedPreferences= getSharedPreferences("test", Activity.MODE_PRIVATE); 

// 使用getString方法获得value,注意第2个参数是value的默认值 
String name =sharedPreferences.getString("name", ""); 
String habit =sharedPreferences.getString("habit", ""); 

//使用toast信息提示框显示信息 
Toast.makeText(this, "读取数据如下:"+"\n"+"name:" + name + "\n" + "habit:" + habit,Toast.LENGTH_LONG).show(); 

3.记住密码案例

布局文件:

<LinearLayout 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"
    android:orientation="vertical"
    android:background="#f4efef"
    tools:context="com.example.dfcn.paopao.LoginActivity">
   <LinearLayout
       android:background="#f9f74922"
       android:layout_width="match_parent"
       android:layout_height="45dp">
       <LinearLayout
           android:layout_gravity="center_vertical"
           android:layout_weight="1"
           android:layout_width="10dp"
           android:layout_height="wrap_content">
           <ImageView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:src="@mipmap/back_btn"/>
       </LinearLayout>

<LinearLayout
    android:layout_weight="1"
    android:layout_gravity="center_vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:textColor="#ffff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="登 陆"
        android:textSize="25sp" />
</LinearLayout>

   </LinearLayout>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="15dp">

</LinearLayout>
<LinearLayout
    android:background="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:orientation="vertical">
    <RelativeLayout
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="75dp">
        <TextView
            android:id="@+id/tv_number"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手机:"/>
        <EditText
            android:id="@+id/login_iphone_et"
            android:layout_toRightOf="@id/tv_number"
            android:hint="请输入手机号"
            android:background="#00000000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/tv_number"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:background="#dedddd"
            android:layout_width="match_parent"
            android:layout_height="1dp" />
    </RelativeLayout>

    <RelativeLayout
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="75dp">
        <TextView
            android:id="@+id/tv_pwd"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"/>
        <EditText
            android:id="@+id/login_pwd_et"
            android:layout_toRightOf="@id/tv_pwd"
            android:hint="请输入密码"
            android:background="#00000000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@id/tv_pwd"/>
    </RelativeLayout>

</LinearLayout>
    <CheckBox
        android:id="@+id/cb_pwd"
        android:text="记住密码"
        android:layout_width="match_parent"
        android:layout_height="30dp"/>
<LinearLayout
    android:layout_marginTop="30sp"
    android:gravity="center"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
   <RelativeLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
     <Button
         android:textColor="#ffff"
         android:background="#f9f74922"
         android:id="@+id/btn_login"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="登陆"
         />
       <Button
           android:textColor="#ffff"
           android:background="#f9f74922"
           android:id="@+id/btn_register"
           android:layout_marginLeft="80dp"
           android:layout_toRightOf="@id/btn_login"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="注册"/>
   </RelativeLayout>
</LinearLayout>


</LinearLayout>

activity:

import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
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;

public class passwordActivity extends AppCompatActivity {
private EditText iphonenum,pwd;
private Button loginbtn;
private CheckBox checkBox;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        iphonenum=findViewById(R.id.login_iphone_et);
        pwd=findViewById(R.id.login_pwd_et);
        loginbtn=findViewById(R.id.btn_login);
        checkBox=findViewById(R.id.cb_pwd);
        sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
        //CheakBox的状态
        boolean isRemenber=sharedPreferences.getBoolean("checkBox",false);
        //如果勾选了下次在进入就把保存的数据给读取出来
        if (isRemenber==true){
            String usename=sharedPreferences.getString("usename","");
            String pwds=sharedPreferences.getString("pwds","");
            iphonenum.setText(usename);
            pwd.setText(pwds);
            checkBox.setChecked(true);
        }
        //登陆的单击事件
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String usename=iphonenum.getText().toString();
                String pwds=pwd.getText().toString();
                editor=sharedPreferences.edit();
                //判断是否勾上了记住密码
               if (checkBox.isChecked()){
                   editor.putString("usename",usename);
                   editor.putString("pwds",pwds);
                   editor.putBoolean("checkBox",true);
               }
               else {
                   editor.clear();
               }
                    editor.commit();
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
                finish();

            }
        });
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值