Android 记录登陆时候的用户名和密码

简述:

在登陆的时候,有时候会遇到一个勾选框,用来询问是否保留用户名和密码


知识点:

SharedPreferences

用来访问程序的文件,从里面读出用户名和密码



代码实现:

MyPreference.java

package com.aimp.help;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 
 * 记录用户名,密码之类的首选项
 *
 */
public class MyPreference {
	private static MyPreference preference = null;
	private SharedPreferences sharedPreference;
	private String packageName = "";
	
	private static final String LOGIN_NAME = "loginName"; //登录名
	private static final String PASSWORD = "password";  //密码
	private static final String IS_SAVE_PWD = "isSavePwd"; //是否保留密码
	
	public static synchronized MyPreference getInstance(Context context){
		if(preference == null)
			preference = new MyPreference(context);
		return preference;
	}
	
	
	public MyPreference(Context context){
		packageName = context.getPackageName() + "_preferences";
		sharedPreference = context.getSharedPreferences(
				packageName, context.MODE_PRIVATE);
	}
	
	
	public String getLoginName(){
		String loginName = sharedPreference.getString(LOGIN_NAME, "");
		return loginName;
	}
	
	
	public void SetLoginName(String loginName){
		Editor editor = sharedPreference.edit();
		editor.putString(LOGIN_NAME, loginName);
		editor.commit();
	}
	
	
	public String getPassword(){
		String password = sharedPreference.getString(PASSWORD, "");
		return password;
	}
	
	
	public void SetPassword(String password){
		Editor editor = sharedPreference.edit();
		editor.putString(PASSWORD, password);
		editor.commit();
	}
	
	
	public boolean IsSavePwd(){
		Boolean isSavePwd = sharedPreference.getBoolean(IS_SAVE_PWD, false);
		return isSavePwd;
	}
	
	
	public void SetIsSavePwd(Boolean isSave){
		Editor edit = sharedPreference.edit();
		edit.putBoolean(IS_SAVE_PWD, isSave);
		edit.commit();
	}
}


LoginActivity.java

package com.aimp.ui;

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

import com.aimp.R;
import com.aimp.help.MyPreference;

public class LoginActivity extends Activity implements OnClickListener {
	public final static String TAG = "LogingActivity";

	private EditText et_loginName, et_password;
	private Button bt_login;
	private CheckBox chk_keep_pwd;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		setContentViewID(R.layout.activity_login);
	}

	public void setContentViewID(int viewId) {
		setContentView(viewId);
		initView();
	}


	public void initView() {
		et_loginName = (EditText) findViewById(R.id.username);
		et_password = (EditText) findViewById(R.id.password);
		bt_login = (Button) findViewById(R.id.login);
		bt_login.setOnClickListener(this);
		chk_keep_pwd = (CheckBox) findViewById(R.id.store_pwd);
		chk_keep_pwd.setChecked(
				MyPreference.getInstance(mContext).IsSavePwd());
	}


	@Override
	protected void onResume() {
		super.onResume();
		mContext = this;
		chk_keep_pwd.setChecked(MyPreference.getInstance(mContext).IsSavePwd());
		if (chk_keep_pwd.isChecked()) {
			et_loginName.setText(MyPreference.getInstance(mContext)
					.getLoginName());
			et_password
			.setText(MyPreference.getInstance(mContext).getPassword());
		}
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()){
		//登陆的时候,根据是否保留登陆名保留登录名,密码
		case R.id.login:
			if(chk_keep_pwd.isChecked()){
				MyPreference.getInstance(mContext)
				    .SetLoginName(et_loginName.getText().toString().trim());
				MyPreference.getInstance(mContext)
				    .SetPassword(et_password.getText().toString().trim());
				MyPreference.getInstance(mContext)
				    .SetIsSavePwd(chk_keep_pwd.isChecked());
			}else{
				MyPreference.getInstance(mContext)
				    .SetLoginName("");
				MyPreference.getInstance(mContext)
				    .SetPassword("");
				MyPreference.getInstance(mContext)
				    .SetIsSavePwd(chk_keep_pwd.isChecked());
			}
			Intent intent = new Intent(LoginActivity.this, SecondActivity.class);
			startActivity(intent);
			break;
		case R.id.store_pwd:
			chk_keep_pwd.setChecked(!chk_keep_pwd.isChecked());
			break;
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值