SharedPreference存储实战之记住登陆账号密码

        数据持久化就是指将那些内存中的瞬时数据保存到持久化设备中(如手机文件、数据库等),当关机,停电后,数据不丢失。  Android 系统中主要提供了三种方式用于实现数据持久化功能,分别是:  1、文件存储  2、SharedPreference 存储  3、数据库存储。

今日使用SharedPreference存储实现记住登陆账号密码的功能:


效果图:



activity_main.xml中代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆成功,进入主页面"
        android:textSize="30sp" />

    <Button
        android:id="@+id/forceExit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="强制退出" />

</LinearLayout>

MainActivity中代码:

package com.demo.rememberaccountdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends BaseActivity {

	private Button forceExit;

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

		forceExit = (Button) findViewById(R.id.forceExit);
		forceExit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 销毁所有的活动
				ActivityCollector.finishAll();
				// 从主页面跳转到登录页面
				Intent intent = new Intent(MainActivity.this,
						LoginActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}
}



login.xml中代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

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

        <EditText
            android:id="@+id/userName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="输入用户名" >

            <requestFocus />
        </EditText>
    </LinearLayout>

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

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

        <EditText
            android:id="@+id/password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="6~16位数字、密码" >

            <requestFocus />
        </EditText>
    </LinearLayout>

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

        <CheckBox
            android:id="@+id/remember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码" />
    </LinearLayout>

    <Button
        android:id="@+id/login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登陆" />

</LinearLayout>

LoginActivity中的代码:


package com.demo.rememberaccountdemo;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends BaseActivity {
	private SharedPreferences preferences;
	private SharedPreferences.Editor editor;
	private EditText userNameText;
	private EditText passwordText;
	private CheckBox remember;
	private Button login;
	private String TAG = "LoginActivity" ;

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

		userNameText = (EditText) findViewById(R.id.userName);
		passwordText = (EditText) findViewById(R.id.password);
		remember = (CheckBox) findViewById(R.id.remember);
		login = (Button) findViewById(R.id.login);
		preferences = PreferenceManager.getDefaultSharedPreferences(this);

		boolean isRemember = preferences.getBoolean("remember", false);
		if (isRemember) {
			// 将账号和密码加载到文本框中
			String userName = preferences.getString("userName", "");
			String password = preferences.getString("password", "");
			userNameText.setText(userName);
			passwordText.setText(password);
			remember.setChecked(true);
		}
		login.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// 获得用户名和密码
				String userName = userNameText.getText().toString();
				String password = passwordText.getText().toString();
				// 判断用户名和密码是否正确
				if (userName.equals("admin") && password.equals("123456")) {
					editor = preferences.edit();
					if (remember.isChecked()) {
						Log.d(TAG, "come in remember") ;
						// 检查复选框是否被选中,选中则将数据放到editor中
						editor.putBoolean("remember", true);
						editor.putString("userName", userName);
						editor.putString("password", password);
					} else {
						Log.d(TAG, " not come in remember") ;
						editor.clear();
					}
					editor.commit();// 提交数据
					// 登陆成功,跳转到主页面
					Log.d(TAG, " go to MainActivity") ;
					Intent intent = new Intent(LoginActivity.this,
							MainActivity.class);
					startActivity(intent);
					finish();
				} else {
					Toast.makeText(LoginActivity.this, "用户名密码错误", 1).show();
				}
			}
		});

	}
}

由于页面的原因,ActivityCollector中的代码,BaseActivity中的代码,AndroidManifest.xml中的代码,就不贴出来了,想看的,下载demo里面有,


demo下载点这里:SharedPreference存储实战之记住登陆账号密码


保存前截图:


保存后截图:


保存值截图:






  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值