数据储存--SharedPreferences

  • SharedPreferences是一种轻型的数据储存方式,其本质是给予XML文件储存key-value键值对数据,通常用来存储一些简单的配置信息。
  • SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
实现SharedPreferences存储的步骤如下:
1).获得SharedPreferences对象;
SharedPreferences prf = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
//SharedPreferences prf = getSharedPreferences("Myprf",MODE_PRIVATE);或用此方法也可获得SharedPreferences对象
2).获得SharedPreferences.Editor对象;
SharedPreferences.Editor editor = prf.edit();
3)通过Editor接口的putxxx方法保存key-value(xxx表示数据类型:String;int..); 
editor.putString("name","隔壁老王");
editor.putInt("age",48);
editor.putBoolean("猥琐",true);    
4).通过Editor的commit方法保存key-value
editor.commit();
* 每次commit()后其上的操作才有效,通过getxxx()取值。
实例:
activity_main.xml
<RelativeLayout 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"
    tools:context="com.example.administrator.sharedpreferencestest.MainActivity">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="昵称:"
        android:layout_alignParentLeft="true"
        android:id="@+id/name"
        android:paddingTop="18dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/name"
        android:id="@+id/ipname"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:text="密码:"
        android:id="@+id/code"
        android:paddingTop="22dp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ipcode"
        android:layout_toRightOf="@+id/code"
        android:layout_below="@+id/ipname"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/login"
        android:text="登陆"
        android:layout_marginLeft="40dp"
        android:layout_marginStart="40dp"
        android:layout_below="@+id/ipcode"
        android:layout_alignLeft="@+id/ipcode"
        android:layout_alignStart="@+id/ipcode"
        android:layout_marginTop="50dp"
        android:onClick="doClick"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cancel"
        android:text="取消"
        android:layout_alignTop="@+id/login"
        android:layout_toRightOf="@+id/login"
        android:layout_toEndOf="@+id/login"
        android:layout_marginLeft="70dp"
        android:layout_marginStart="70dp"
        android:onClick="doClick"/>
    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存昵称"
        android:id="@+id/sv_name"
        android:layout_below="@+id/ipcode"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

MainActivity.java
public class MainActivity extends AppCompatActivity {
    EditText ipName,ipCode;
    CheckBox sv_name;
    Button login,cancel;
    SharedPreferences pref;
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ipName = (EditText) findViewById(R.id.ipname);
        ipCode = (EditText) findViewById(R.id.ipcode);
        sv_name = (CheckBox) findViewById(R.id.sv_name);
        login = (Button) findViewById(R.id.login);
        cancel = (Button) findViewById(R.id.cancel);
        pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
        editor  = pref.edit();
        String name = pref.getString("name","");
        if (name.equals("")){
            sv_name.setChecked(false);
        }
        else{
            sv_name.setChecked(true);
            ipName.setText(name);
        }
    }
    public  void  doClick(View v){
        switch (v.getId()){
            case R.id.login:
                String name = ipName.getText().toString().trim();
                String code = ipCode.getText().toString().trim();
                if ("admin".equals(name)&&"123456".equals(code)){
                    if (sv_name.isChecked()){
                        editor.putString("name",name);
                        editor.commit();
                    }
                    else{
                        editor.remove("name");
                        editor.commit();
                    }
                }
                else {
                    Toast.makeText(MainActivity.this,"密码错误",Toast.LENGTH_LONG).show();
                }
                Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
                break;
        }
    }
}

第一次启动程序
输入昵称密码并保存昵称
再次启动程序可以看到昵称已被保存
本文主要作为熟悉了解SharedPreferences存储机制及使用方法,此登录注册界面要完善的话可完善的地方不要太多,无奈还很菜,以后能力足够了再专门开登陆界面0.0~

本文为学习笔记,参考慕课网视频整理而来,视频地址:http://www.imooc.com/learn/179。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值