• Android笔记(三十八) Android中的数据存储——SharedPreferences


          SharedPreferences是Android提供的一种轻型的数据存储方法,其本质是基于xml文件存储的,内部数据以key-value的方式存储,通常用来存储一些简单的配置信息。

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

    使用SharedPreperences保存数据

          使用SharedPreperences来保存数据的步骤如下:

          1. 实例化SharedPreperences对象

          2. 实例化Editor对象

          3. editor.put方法保存数据

          4. editor.commit提交数据

          例子:

    MainActivity.java

    package cn.lixyz.sharedpreferencesdemo;
    
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        private EditText username;
        private EditText password;
        private Button login;
        private Button register;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            username = (EditText) findViewById(R.id.username);
            password = (EditText) findViewById(R.id.password);
            login = (Button) findViewById(R.id.login);
            register = (Button) findViewById(R.id.register);
    
    
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharedPreferences sp = getSharedPreferences("username", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sp.edit();
                    editor.putString("username", username.getText().toString());
                    editor.putString("password", password.getText().toString());
                    if (editor.commit()) {
                        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="请输入您的用户名"
            android:textSize="30dp" />
    
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="请输入您的密码"
            android:password="true"
            android:textSize="30dp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="登录" />
    
            <Button
                android:id="@+id/register"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="注册" />
        </LinearLayout>
    
    </LinearLayout>

      运行结果:

      我们去DDMS中查找有没有一个username.xml文件,果然有:

      导出打开:

    <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
    <map>
        <string name="username">WWWADS</string>
        <string name="password">WASDFGHA</string>
    </map>

      可以看到我们刚才输入的用户名和密码保存成功了。

      我们使用put存储数据的时候我们会看到

      由此可见,SharedPreferences只能够存储一些基本数据类型的数据。

    使用SharedPreperences来读取数据

          我们来模拟一个记住用户名的操作:

    MainActivity.java

    package cn.lixyz.sharedpreferencesdemo;
    
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        private EditText username;
        private EditText password;
        private Button login;
        private Button register;
        private CheckBox remberUser;
        private TextView forgetPassword;
        SharedPreferences sp ;
        SharedPreferences.Editor editor;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            username = (EditText) findViewById(R.id.username);
            password = (EditText) findViewById(R.id.password);
            login = (Button) findViewById(R.id.login);
            register = (Button) findViewById(R.id.register);
            remberUser = (CheckBox) findViewById(R.id.remberUser);
            forgetPassword = (TextView) findViewById(R.id.forgetPassword);
    
    
            sp = getSharedPreferences("username", MODE_PRIVATE);
            editor = sp.edit();
    
            String spUsername = sp.getString("username","");
            if(spUsername == null){
                remberUser.setChecked(false);
            }else{
                username.setText(spUsername);
                remberUser.setChecked(true);
            }
    
    
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(remberUser.isChecked()){
                        editor.putString("username", username.getText().toString());
                        if (editor.commit()) {
                            Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            });
        }
    }

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="请输入您的用户名"
            android:textSize="30dp" />
    
        <EditText
            android:id="@+id/password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="请输入您的密码"
            android:password="true"
            android:textSize="30dp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="登录" />
    
            <Button
                android:id="@+id/register"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="注册" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="10dp">
            <CheckBox
                android:id="@+id/remberUser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="记住用户名"/>
    
            <TextView
                android:id="@+id/forgetPassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="180dp"
                android:text="忘记密码"/>
        </LinearLayout>
    
    </LinearLayout>

      

  • 相关阅读:
    CURL 命令
    进入容器时给容器root权限
    Nginx性能优化功能- Gzip压缩(大幅度提高页面加载速度)
    Linux防火墙Firewall和Iptables的使用
    Linux 下统计文件的个数
    linux命令: wc命令
    Java static关键字详解
    Jmeter 定时器
    Java数据类型转换:强制类型转换+自动类型转换
    Jmeter中正则表达式提取器使用详解
  • 原文地址:https://www.cnblogs.com/xs104/p/4884956.html
Copyright © 2020-2023  润新知