简述:
在登陆的时候,有时候会遇到一个勾选框,用来询问是否保留用户名和密码
知识点:
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;
}
}
}