Android基础--键值对存储(SharedPreferences)

SharedPreferences用于将键值对形式的数据存储到当前应用专属的存储空间中

package com.itheima.sharedpreferences;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
    private EditText et_username;
    private EditText et_password;
    private CheckBox cb_isAutoLogin;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        cb_isAutoLogin = (CheckBox) findViewById(R.id.isAutoLogin);
        readAccount();
    }
    
  //如果之前用户将信息存储到手机上,需要对用户信息进行回显
      private void readAccount() {
          SharedPreferences sPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);
          String username =sPreferences.getString("username", "");
          String password = sPreferences.getString("password", "");
          et_username.setText(username);
          et_password.setText(password);
      }
 // 登录 ,将用户名和密码保存到内部存储空间
     public void login(View v) {
         String username = et_username.getText().toString();
         String password = et_password.getText().toString();
         if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
             Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
             return;
         }
         if (cb_isAutoLogin.isChecked()) {
             //文件名不用写扩展名,会自动添加xml扩展名
               /*
                * name Desired preferences file. 
                * If a preferences file by this name does not exist, it will be created 
                * when you retrieve an editor (SharedPreferences.edit()) and then commit changes (Editor.commit()).
                */
             SharedPreferences sPreferences = getSharedPreferences("userInfo", MODE_PRIVATE);
             Editor editor = sPreferences.edit();
             editor.putString("username", username);
             editor.putString("password", password);
             editor.commit();
         }
         Toast.makeText(this, "登录成功", 0).show();
     }
    
}

文件存储结构如下图:

 

布局文件:

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/_username" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/_password" 
        android:inputType="textPassword"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/isAutoLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="@string/_autologin" 
            android:layout_centerVertical="true"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="@string/_login" 
            android:onClick="login"/>
    </RelativeLayout>

</LinearLayout>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。