Android数据存储

什么是数据存储
运行内存类似于电脑内存
机身内部存储器类似于电脑硬盘
并且手机中所有的文件,都存储在机身的内部存储器当中
手机内部存储器(核心文件夹)
1./system系统文件夹,类似于Windows中的Windows文件夹
2./data应用程序安装文件和数据文件(存放在这个目录下的数据文件名称之为内部存储)
3./mnt/sdcard 外部存储根文件夹,用于存放各种文件(存放在此目录中的文件称之为外部存储)。
4./storage/emulated/0外部存储根文件夹,有些手机放在/mnt/sdcard文件夹下,有一些手机存放在/storage/emulated/0下
在商业APP中经常需需要手机用户的信息,保存配置信息等,这些数据必须以某种方式保存,不能丢失,并且能够有效,简洁地使用和更新处理,这种保存数据的方式称之为 数据存储。
关于数存储的分类包括
SharedPreferences存储,File存储,数据库存储
SharedPreferences常用来存储一些轻量级的数据,以键值对形式存储数据,当用户卸载应用程序时,数据一会清除。
什么是SharedPreferences

SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出。
SharedPreferences提供了java常规的Long、Int、String等类型数据的保存接口。 [1] SharedPreferences类似过去Windows系统上的ini配置文件,但是它分为多种权限,可以全局共享访问。
提示最终是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比SQLite要好不少,如果真的存储量不大可以考虑自己定义文件格式。xml处理时Dalvik会通过自带底层的本地XML Parser解析,比如XMLpull方式,这样对于内存资源占用比较好。

对于SharedPreferences的使用方法。
1.获得试用SharedPreferences对象
2.获得Editor对象
3.通过Editor对象的putxxx函数,设置写入的数据
4.通过Editor对象的commit提交写入

getSharedPreferences方法的参数:
1.name:要保存的文件名
2。mode Context:MODE_PRIVATE:默认操作模式,代表该文章是私有数据,只能被应用本身访问,在该模式下,写入的文件就会覆盖原来的文件夹。
3.Context:MODE_APPEND:该模式会检查文件是否存在,存在就追加内弄,否则就创建新文件夹
之后我们就来代码展示下面的就是xml布局

<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"
    android:orientation="vertical"
    tools:context="com.example.myapplication.MainActivity">

    <EditText
        android:id="@+id/uesrname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入账号"
        />
    <EditText
        android:id="@+id/passname"
        android:layout_below="@+id/uesrname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码"
        android:inputType="textPassword"
        />
    <CheckBox
        android:id="@+id/checkBox"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/passname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="记住密码"/>
    <Button
        android:id="@+id/button_pass"
        android:layout_below="@+id/checkBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击登录"/>


</RelativeLayout>

之后我们来看看MainActivity的代码段


import android.content.SharedPreferences;
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.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button button;
    private TextView userText;
    private TextView passText;
  //  private CheckBox checkBox;
    private int rememberFlge=0;
    private CheckBox rememberPswck;
    String password="";



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


        blid();

        SharedPreferences sharedPreferences = getSharedPreferences("my.xml",MODE_PRIVATE);
        if (sharedPreferences!=null){
        //判断是否有数据有的话就把他放在EditText中
            String name = sharedPreferences.getString("name","");
            password = sharedPreferences.getString("password","");
            //给这个 rememberFlge初始化
            rememberFlge=sharedPreferences.getInt("remember_flag",0);
        //将账号保存在界面上
            userText.setText(name);


        }
        //接受返回值1 之后将密码保存在界面上
        if (rememberFlge==1){
            rememberPswck.setChecked(true);
            passText.setText(password);

        }

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String username = userText.getText().toString();
                String password = passText.getText().toString();



          //在界面上输入自己的密码然后保存
                SharedPreferences spf = getSharedPreferences("my.xml",MODE_PRIVATE);
                SharedPreferences.Editor editor =spf.edit();
                editor.putString("name",username);
                //判断是否打钩 有的话就返回1
              if (rememberPswck.isChecked()){
                   rememberFlge=1;
                   //并且输入自己的密码
                   editor.putInt("remember_flag",rememberFlge);
                   editor.putString("password",password);
              }else {
              //没有的话就返回0
                  rememberFlge=0;
                  editor.putInt("remember_flag",rememberFlge);
              }




                Toast.makeText(MainActivity.this,"登录成功", Toast.LENGTH_SHORT).show();
                editor.commit();
//




            }
        });
    }

    private void blid() {
        button=findViewById(R.id.button_pass);
        userText=findViewById(R.id.uesrname);
        passText=findViewById(R.id.passname);
        rememberPswck=findViewById(R.id.checkBox);
    }
}

这就是关于SharedPreferences使用方法

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值