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

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;

	@Override
	protected 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", ""));	
	}

	@Override
	public 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;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (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;
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		switch (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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值