Android攻城狮的第二门课(第3季)第1章 数据存储之SharedPreferences

Android的四中数据存储方法:
1、SharedPreferences
2、SQLite
3、Content Provider
4、File

SharedPreferences:(效率不高,ASCII码文件)
1、是一种轻型的数据存储方式
2、本质是基于XML文件存储key-value键值对数据
3、通常用来存储以一些简单的配置信息(配置文件)
如:重载窗口状态、下次密码登陆。

SharedPreferences对象本身只能获取数据而不支持存储和修改,
存储修改是通过Editor对象实现。

实现SharedPreferences存储的步骤:
(1)获得SharedPreferences对象
(2)获得SharedPreferences.Editor对象
(3)通过Editor接口的putXxx方法保存key-value对其中Xxx表示不同的数据类型。

package com.imooc.li806.sharedpreferencesdemo1;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        //参数:1.文件(保存数据)。2.文件的权限(MODE_PRIVATE只有当前程序可以修改)
        SharedPreferences pref = getSharedPreferences("myPerf",MODE_PRIVATE);
        //写入
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("name","张三");
        editor.putInt("age",30);
        editor.putLong("time", System.currentTimeMillis());
        editor.putBoolean("default",true);
        //提交
        editor.commit();
        //删除
        editor.remove("default");
        //每次操作完之后都要进行提交
        editor.commit();
        //打印信息
        System.out.println(pref.getString("name",""));
        System.out.println(pref.getInt("age",0));

    }
}
package com.imooc.li806.sharedpreferencesdemo1;

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.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    EditText etUserName,etUserPass;
    CheckBox chk;
    SharedPreferences pref;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
/*
        //SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        //参数:1.文件(保存数据)。2.文件的权限(MODE_PRIVATE只有当前程序可以修改)
        SharedPreferences pref = getSharedPreferences("myPerf",MODE_PRIVATE);
        //写入
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("name","张三");
        editor.putInt("age",30);
        editor.putLong("time", System.currentTimeMillis());
        editor.putBoolean("default",true);
        //提交
        editor.commit();
        //删除
        editor.remove("default");
        //每次操作完之后都要进行提交
        editor.commit();
        //打印信息
        System.out.println(pref.getString("name",""));
        System.out.println(pref.getInt("age",0));
*/
        etUserName = (EditText) findViewById(R.id.etusername);
        etUserPass = (EditText) findViewById(R.id.etuserpass);
        chk = (CheckBox) findViewById(R.id.chkSaveName);
        pref = getSharedPreferences("UserInfo",MODE_PRIVATE);
        editor = pref.edit();//启用编辑
        String name = pref.getString("userName","");
        if(name == null){//如果名字不为空chk位true
            chk.setChecked(false);
        }else{
            chk.setChecked(true);
            etUserName.setText(name);
        }

    }
    public void doClick(View V){
        switch (V.getId()){   //获取ID
            case R.id.btnLogin:
                //.trim()去掉字符串的首位空格
                String name = etUserName.getText().toString().trim();
                String pass = etUserPass.getText().toString().trim();
                if("admin".equals(name) && "123456".equals(pass)){
                    if(chk.isChecked()){
                        editor.putString("userName",name);
                        editor.commit();
                        Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                    }else{
                        editor.remove("userName");
                        editor.commit();
                    }
                }else{
                    Toast.makeText(MainActivity.this,"禁止登陆",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}
<?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="com.imooc.li806.sharedpreferencesdemo1.MainActivity">


        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginTop="17dp"
            android:text="用户名:" />

        <EditText
            android:id="@+id/etusername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/textView1"
            android:layout_toRightOf="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/textView1"
            android:layout_below="@+id/etusername"
            android:layout_marginTop="16dp"
            android:text="密  码:" />

        <EditText
            android:id="@+id/etuserpass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView2"
            android:layout_alignBottom="@+id/textView2"
            android:layout_alignLeft="@+id/etusername"
            android:ems="10" />

    <CheckBox
            android:id="@+id/chkSaveName"
        android:checked="false"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView2"
            android:layout_below="@+id/textView2"
            android:layout_marginTop="14dp"
            android:text="保存用户名" />

    <Button
            android:id="@+id/btnLogin"
        android:onClick="doClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="登陆"
            android:layout_below="@+id/chkSaveName"
            android:layout_alignLeft="@+id/chkSaveName"
            android:layout_alignStart="@+id/chkSaveName" />

    <Button
        android:id="@+id/btnCancel"
        android:onClick="doClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/btnLogin"
        android:layout_marginLeft="27dp"
        android:layout_marginStart="27dp"
        android:layout_toEndOf="@+id/chkSaveName"
        android:layout_toRightOf="@+id/chkSaveName"
        android:text="取消" />


</RelativeLayout>

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值