安卓SharedPrefences详解以及记住密码的小例子

来源:互联网 发布:网络延长器多少钱 编辑:程序博客网 时间:2024/05/22 02:12

1.Sharedfrefences的作用

SharedPreferences是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。
存储在/data/data/<包名>/shared_prefs目录下。

2.用SharedPrefences存储数据

Context context = MainActivity.this;
SharedPreferences sharedPreferences = context.getSharedPreferences("MY_PRE", MODE_PRIVATE);//执行完成后会在/data/data/<包名>/shared_prefs目录下生成一个MY_PRE.xml文件
Editor editor = sharedPreferences.edit();//需要用Editor
editor.putString("user", userString);
editor.putString("password", passwordString);
editor.commit();//记得要commit一下

3.从SharedPrefences中取数据

用sharedPreferences.getString("user", "")取出key为"user"的值,如果该key不存在,则将该key的值赋为“”

4.一个登陆的记住账号密码的小例子


activity_mail.xml
<span style="font-family:Microsoft YaHei;"><RelativeLayout 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: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" >    <LinearLayout android:id="@+id/lin1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_margin="6px"        android:orientation="horizontal">        <TextView android:id="@+id/user"            android:layout_width="150px"            android:layout_height="wrap_content"            android:text="user"/>        <EditText android:id="@+id/myuser"            android:layout_width="fill_parent"            android:layout_height="wrap_content"/>    </LinearLayout>        <LinearLayout android:id="@+id/lin2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_margin="6px"        android:orientation="horizontal"        android:layout_below="@id/lin1">        <TextView android:id="@+id/password"            android:layout_width="150px"            android:layout_height="wrap_content"            android:text="password"/>        <EditText android:id="@+id/mypassword"            android:layout_width="fill_parent"            android:layout_height="wrap_content"/>    </LinearLayout>        <Button android:id="@+id/login"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="Login"        android:layout_below="@id/lin2"/>        <LinearLayout android:id="@+id/lin3"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_margin="6px"        android:orientation="horizontal"        android:layout_below="@id/login">        <CheckBox android:id="@+id/remember"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:checked="true"            android:text="记住密码?"/>            </LinearLayout>        </RelativeLayout></span>

MainAcitvity----如果用户名是zyr,密码是zyr123,则跳到BActivity
<span style="font-family:Microsoft YaHei;">package com.example.sharepreferencestest;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.view.Menu;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 userEditText;private EditText passwordEditText;private Button loginButton;private CheckBox rCheckBox;private String userString;private String passwordString;private SharedPreferences sharedPreferences;private Editor editor;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);userEditText = (EditText)findViewById(R.id.myuser);passwordEditText = (EditText)findViewById(R.id.mypassword);loginButton = (Button)findViewById(R.id.login);rCheckBox = (CheckBox)findViewById(R.id.remember);loginButton.setOnClickListener(this);rCheckBox.setOnCheckedChangeListener(this);//获取SharedPreferences对象Context context = MainActivity.this;sharedPreferences = context.getSharedPreferences("MY_PRE", MODE_PRIVATE);editor = sharedPreferences.edit();//userEditText.setText(sharedPreferences.getString("user", ""));passwordEditText.setText(sharedPreferences.getString("password", ""));}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.login:userString = userEditText.getText().toString();passwordString = passwordEditText.getText().toString();//如果记住用户密码的话,在点击登录按钮时,写数据if(rCheckBox.isChecked()){editor.putString("user", userString);editor.putString("password", passwordString);editor.commit();}if(userString.equals("zyr")&&passwordString.equals("zyr123")){Intent intent = new Intent();intent.setClass(MainActivity.this, BActivity.class);startActivity(intent);}break;default:break;}}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubswitch (buttonView.getId()) {case R.id.remember:System.out.println(isChecked);if(isChecked){//Toast.makeText(this, "checked", Toast.LENGTH_SHORT).show();}break;default:break;}}}</span>
BActivity
<span style="font-family:Microsoft YaHei;">package com.example.sharepreferencestest;import android.app.Activity;import android.os.Bundle;public class BActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.b);}}</span>

b.xml
<span style="font-family:Microsoft YaHei;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <TextView android:id="@+id/btext"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="welcom!"        android:gravity="center"        android:textSize="30sp"/>    </LinearLayout></span>




0 0
原创粉丝点击