Android中数据存储之一------SharedPreferences

来源:互联网 发布:exwinner成套报价软件 编辑:程序博客网 时间:2024/05/18 20:36


       SharedPreferences是android.content包中的一个接口,提供来访问和修改偏好数据。其处理的是KEY-VALUES类型的数据。其数据存储位置在/data/data/shared-pref文件夹中,文件的保存格式是.xml格式,导出该文件发现其中确实是以键值对的方式存储的。

       SharedPreferences可以存储的数据类型有:

       1   简单类型,如string ,int

       2   复杂的数据类型(对象、图像),必须经过编码处理,然后将编码后的数据以字符串的形式存储。

       虽然可以用其保存复杂数据类型,但推荐当要存取更多的数据时,采用文件或SQLite.

       sharedPreferences 对数据的操作主要通过它的一个内部接口Editor来完成。Editor提供了对各种数据类型的写操作,以签名putXX(,)形式。注意不管是写入还是移除数据,最后都要调用Editor.commit()方法,进行提交。

      下面看看具体实现:

Activity的代码:

package com.luise.datastorage;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,
  OnCheckedChangeListener {

 // 声明控件变量
 private EditText mUserName;
 private EditText mUserPassword;
 private CheckBox mSaveUser;
 private Button mBtnLogin;
 private String mStrUserName, mStrUserPassword;
 private SharedPreferences msharePreferences;

 private final static String USERNAME = "userName";
 private final static String PASSWORD = "userPassword";

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

              mUserName = (EditText) findViewById(R.id.et_userName);
              mUserPassword = (EditText) findViewById(R.id.et_userPassword);
              mSaveUser = (CheckBox) findViewById(R.id.cb_saveUser);
              mBtnLogin = (Button) findViewById(R.id.btn_login);

              mBtnLogin.setOnClickListener(this);
              mSaveUser.setOnCheckedChangeListener(this);

 }

 @Override
 public void onCheckedChanged(CompoundButton cbtn, boolean isChecked) {

               mStrUserName = mUserName.getText().toString().trim();
               mStrUserPassword = mUserPassword.getText().toString().trim();

               Editor editor = null; // 得到sharedPerferences对象

              msharePreferences =getSharedPreferences("user", MODE_PRIVATE);
              editor = msharePreferences.edit();

              if (isChecked) {

                        editor.putString(USERNAME, mStrUserName);
                        editor.putString(PASSWORD, mStrUserPassword);

                        editor.commit();
             } else {
             editor.remove(USERNAME);
             editor.remove(PASSWORD);

             editor.commit();
           }

 }

 @Override
 public void onClick(View v) {
          switch (v.getId()) {
         case R.id.btn_login:
                 mStrUserName = mUserName.getText().toString().trim();
                 mStrUserPassword = mUserPassword.getText().toString().trim();

                 if (mStrUserName.equals("ly")
                 && mStrUserPassword.equals("12345")) {
                 Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

                } else {
                Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                }
                break;

          }

 }

 @Override
 protected void onResume() {
              super.onResume();

             msharePreferences = getSharedPreferences("user", MODE_PRIVATE);
             String strUserName = msharePreferences.getString(USERNAME, null);
             String strUserPassword = msharePreferences.getString(PASSWORD, null);

              

            if (strUserName == null || strUserPassword == null) {
                      mSaveUser.setChecked(false);
             } else if (strUserName.equals("liyong") && strUserPassword.equals("12345") ) {
                      mUserName.setText(strUserName);
                      mUserPassword.setText(strUserPassword);
                      mSaveUser.setChecked(true);

         Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

           } else {
           Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名:" />

    <EditText
        android:id="@+id/et_userName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:" />

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

    <CheckBox
        android:id="@+id/cb_saveUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住用户名和密码" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录" />

</LinearLayout>




0 0
原创粉丝点击