码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-11-21 01:22:34      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:des   android   style   blog   http   io   ar   color   os   

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();
     }
    
}

文件存储结构如下图:

bubuko.com,布布扣

 

布局文件:

<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>

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

标签:des   android   style   blog   http   io   ar   color   os   

原文地址:http://www.cnblogs.com/fujianyi/p/4111848.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!