Android学习-SharedPreferences

Android的四种数据存储方式:
1>SharedPreferences
2>SQLite
3>Content provider
4>File

SharedPreferences
1.是一种轻型的数据存储方式
2.本质是基于XML文件存储key-value键值对的数据
3.通常用来存储一些简单的配置信息
4.SharedPreferences对象本身只能获取数据而不支持存储和修改,存储和修改是通过Editor对象实现。
5.实现SharedPreferences存储的步骤如下:
1) 获得SharedPreferences对象
2) 获得SharedPreferences.Editor对象
3) 通过Editor接口的putXXX方法保存key-value对,其中XXX表示不同的数据类型。

package com.example.angel.sharedpreferencespro;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {

    private SharedPreferences spf;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        spf = getSharedPreferences("myPref",MODE_PRIVATE);//设置文件名字和权限
        SharedPreferences.Editor e = spf.edit();
        e.putString("name","张三");
        e.putInt("age",25);
        e.putBoolean("isMan",true);
        e.commit();//每次修改都要提交事务

        e.remove("isMan");
        e.commit();
        System.out.println(spf.getString("name",null));//第二个参数是缺省的返回值
        System.out.println(spf.getInt("age",0));
    }
}

例子:
完成用户名与密码保存

MainActivity.jav

public class MainActivity extends AppCompatActivity {

    private SharedPreferences spf;
    private Button okBtn;
    private Button cancelBtn;
    private EditText username_txt;
    private EditText password_txt;
    private CheckBox chk;
    SharedPreferences.Editor editor;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        spf = getSharedPreferences("Myspf2",MODE_PRIVATE);//设置文件名权限
        okBtn = (Button) findViewById(R.id.ok);
        cancelBtn = (Button) findViewById(R.id.cancel);
        username_txt = (EditText) findViewById(R.id.username_txt);
        password_txt = (EditText) findViewById(R.id.password_txt);
        chk = (CheckBox) findViewById(R.id.save_chk);
        editor =spf.edit();
        String userName = spf.getString("userName","");
        String password = spf.getString("password","");
        if(userName == null){
            chk.setChecked(false);
        }else{
            chk.setChecked(false);
            username_txt.setText(userName);
            password_txt.setText(password);
        }


    }
    public void doClick(View v){
        String userName = username_txt.getText().toString().trim();
        String password = password_txt.getText().toString().trim();
        switch (v.getId()){
            case R.id.ok:
                if("admin".equals(userName) && "123".equals(password)){
                    if(chk.isChecked()){
                        editor.putString("userName","admin");
                        editor.putString("password","123");
                        editor.commit();
                    }else{
                        editor.remove("userName");
                        editor.remove("password");
                        editor.commit();
                    }
                    Toast.makeText(this,"successful",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(this,"bad",Toast.LENGTH_LONG).show();
                }
                break;

        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />
    <EditText
        android:id="@+id/username_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="请输入您的用户名"/>

    <TextView
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

    <EditText
        android:id="@+id/password_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <CheckBox
        android:id="@+id/save_chk"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存用户名" />

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="确定" />

    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="取消" />

</LinearLayout>

这里写图片描述
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值