基本上 大部分的APP 有登陆功能肯定就有记住账号密码 等,让用户可以良好的使用先看代码:
布局文件
package com.farsight.bookstore; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; 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; /** * 登录界面 * * @author Administrator * */ public class LoginActivity extends Activity implements OnClickListener { private EditText etUerName; private EditText etPassWord; private Button buLogin; private Button buExit; private CheckBox cbSavePassword; private CheckBox cbLogin; private String userName; private String passWord; private SharedPreferences sp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initView(); buLogin.setOnClickListener(this); buExit.setOnClickListener(this); cbSavePassword.setOnClickListener(this); cbLogin.setOnClickListener(this); // 显示 保存的账号密码 if (sp.getString("username", "") != null && sp.getString("password", "") != null && sp.getBoolean("isChecked", false)) { etUerName.setText(sp.getString("username", "")); etPassWord.setText(sp.getString("password", "")); cbSavePassword.setChecked(true); } } /** * 初始化界面ID */ private void initView() { etUerName = (EditText) findViewById(R.id.edittext_username); etPassWord = (EditText) findViewById(R.id.edittext_password); buLogin = (Button) findViewById(R.id.button_login); buExit = (Button) findViewById(R.id.button_exit); cbSavePassword = (CheckBox) findViewById(R.id.checkBox_savePassword); cbLogin = (CheckBox) findViewById(R.id.checkBox_login); sp = getSharedPreferences("config", MODE_PRIVATE); } @Override public void onClick(View v) { userName = etUerName.getText().toString().trim(); passWord = etPassWord.getText().toString().trim(); Log.i("main", "username" + userName); Log.i("main", "password" + passWord); switch (v.getId()) { case R.id.button_login: if (isScuess(userName, passWord)) { // 当账号密码正确时 保存正确的信息 sp.edit().putString("username", userName).commit(); sp.edit().putString("password", passWord).commit(); goHomePager();// 跳到主页面 } else { Toast.makeText(this, "账号或密码错误!", Toast.LENGTH_SHORT).show(); } break; case R.id.button_exit: System.exit(0); break; case R.id.checkBox_login: break; case R.id.checkBox_savePassword: if (cbSavePassword.isChecked()) { sp.edit().putBoolean("isChecked", true).commit(); // 提交checkBox // 点击 } else { sp.edit().putBoolean("isChecked", false).commit(); // 提交checkBox // 点击 } break; } } /** * 跳转到主页面 */ private void goHomePager() { Intent intent = new Intent(this, HomeActivity.class); startActivity(intent); } /** * 判断账号密码是否 符合 */ private boolean isScuess(String user, String password) { if (user.equals("admin") && password.equals("admin")) { Log.i("main", "成功"); return true; } return false; } }
登录代码:
package com.farsight.bookstore;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
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;
/**
* 登录界面
*
* @author Administrator
*
*/
public class LoginActivity extends Activity implements OnClickListener {
private EditText etUerName;
private EditText etPassWord;
private Button buLogin;
private Button buExit;
private CheckBox cbSavePassword;
private CheckBox cbLogin;
private String userName;
private String passWord;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
buLogin.setOnClickListener(this);
buExit.setOnClickListener(this);
cbSavePassword.setOnClickListener(this);
cbLogin.setOnClickListener(this);
// 显示 保存的账号密码
if (sp.getString("username", "") != null
&& sp.getString("password", "") != null
&& sp.getBoolean("isChecked", false)) {
etUerName.setText(sp.getString("username", ""));
etPassWord.setText(sp.getString("password", ""));
cbSavePassword.setChecked(true);
}
}
/**
* 初始化界面ID
*/
private void initView() {
etUerName = (EditText) findViewById(R.id.edittext_username);
etPassWord = (EditText) findViewById(R.id.edittext_password);
buLogin = (Button) findViewById(R.id.button_login);
buExit = (Button) findViewById(R.id.button_exit);
cbSavePassword = (CheckBox) findViewById(R.id.checkBox_savePassword);
cbLogin = (CheckBox) findViewById(R.id.checkBox_login);
sp = getSharedPreferences("config", MODE_PRIVATE);
}
@Override
public void onClick(View v) {
userName = etUerName.getText().toString().trim();
passWord = etPassWord.getText().toString().trim();
Log.i("main", "username" + userName);
Log.i("main", "password" + passWord);
switch (v.getId()) {
case R.id.button_login:
if (isScuess(userName, passWord)) {
// 当账号密码正确时 保存正确的信息
sp.edit().putString("username", userName).commit();
sp.edit().putString("password", passWord).commit();
goHomePager();// 跳到主页面
} else {
Toast.makeText(this, "账号或密码错误!", Toast.LENGTH_SHORT).show();
}
break;
case R.id.button_exit:
System.exit(0);
break;
case R.id.checkBox_login:
break;
case R.id.checkBox_savePassword:
if (cbSavePassword.isChecked()) {
sp.edit().putBoolean("isChecked", true).commit(); // 提交checkBox
// 点击
} else {
sp.edit().putBoolean("isChecked", false).commit(); // 提交checkBox
// 点击
}
break;
}
}
/**
* 跳转到主页面
*/
private void goHomePager() {
Intent intent = new Intent(this, HomeActivity.class);
startActivity(intent);
}
/**
* 判断账号密码是否 符合
*/
private boolean isScuess(String user, String password) {
if (user.equals("admin") && password.equals("admin")) {
Log.i("main", "成功");
return true;
}
return false;
}
}
实现记住密码: 需要用到 Android 提供的 SharedPreferences 接口 ,
第一步: 通过 getSharedPreferences() 获取到 对象
第二步: 再通过 对象得到 sp.edit().putBoolean("isChecked", false).commit();
edit 最后别忘了 一个要 commit();