Android实现自动登录和记住密码
2015-12-05 01:42
691 查看
记住密码和自动登录是很多手机软件都有的一个功能。可以用 SharedPreferences类来轻松实现。 SharedPreferences是安卓平台的一个轻量级类。采用 SharedPreferences进行数据存储是很方便的。
public class LoginCheckActivity extends Activity implements OnClickListener{ //登录按钮 private Button loginButton; //账号文本框 private EditText accountEditText; //密码文本框 private EditText passwordEditText; //账号 private String account; //密码 private String password; private String accountValue; private String passwordValue; //进度条 private ProgressDialog pd = null; //记住账号的CheckBox private CheckBox savedAccountCheckBox; //自动登录的CheckBox private CheckBox autoLoginCheckBox; //SharePreferences对象,用于记住账号 private SharedPreferences sp; //记住账号的标志常数 private final String MAK = "innoview"; @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //设置标题不显示 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.login); //账号的文本框 accountEditText = (EditText)this.findViewById(R.id.et_account); //密码的文本框 passwordEditText = (EditText)this.findViewById(R.id.et_password); //保存账号的CheckBox savedAccountCheckBox = (CheckBox)findViewById(R.id.cb_savedAccount); //自动登录的CheckBox autoLoginCheckBox = (CheckBox)findViewById(R.id.cb_autoLogin); //获取保存在SharePreferences里面的账号信息,实现自动登录 sp = getSharedPreferences("accountInfo",Context.MODE_WORLD_READABLE); if(sp.getBoolean("ISCHECK", false)){ savedAccountCheckBox.setChecked(true); try{ accountValue = sp.getString("ACCOUNTVALUE",""); System.out.println("<<<<<<<<<<<<"+"账号"+accountValue); }catch (Exception e) { // TODO: handle exception } accountEditText.setText(account); try{ passwordValue = sp.getString("PASSWORDVALUE",""); System.out.println("<<<<<<<<<<<<"+"密码"+passwordValue); }catch (Exception e) { // TODO: handle exception } passwordEditText.setText(password); if(sp.getBoolean("AUTO_ISCHECK", false)){ autoLoginCheckBox.setChecked(true); initLogin(); } } savedAccountCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { if (savedAccountCheckBox.isChecked()) { System.out.println("记住账号框未选中状态"); sp.edit().putBoolean("ISCHECK", true).commit(); }else { System.out.println("记住账号框未选中"); sp.edit().putBoolean("ISCHECK", false).commit(); } } }); autoLoginCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { if (autoLoginCheckBox.isChecked()) { System.out.println("自动登录功能以启用"); sp.edit().putBoolean("AUTO_ISCHECK", true).commit(); } else { System.out.println("自动登录已关闭"); sp.edit().putBoolean("AUTO_ISCHECK", false).commit(); } } }); loginButton = (Button)findViewById(R.id.bt_login); loginButton.setOnClickListener(this); } /** * 重载的按钮点击事件 */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_login: initLogin(); break; default: break; } } public void initLogin(){ //显示进度条 pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_SPINNER); pd.setMessage("数据加载..."); pd.show(); account = accountEditText.getText().toString(); password = passwordEditText.getText().toString(); if(savedAccountCheckBox.isChecked()) { Editor editor = sp.edit(); try { editor.putString("ACCOUNTVALUE", account); System.out.println("<<<<<<<<"+"账号"+account); } catch (Exception e) { Toast.makeText(LoginCheckActivity.this,"账号保存异常",Toast.LENGTH_SHORT).show(); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } try { editor.putString("PASSWORDVALUE", password); System.out.println("<<<<<<<<"+"密码"+password)); } catch (Exception e) { Toast.makeText(LoginCheckActivity.this,"密码保存异常",Toast.LENGTH_SHORT).show(); e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. } editor.commit(); } loginCheck(account, password); } /** * 登录验证 * @param account 账号(学号) * @param password 密码 */ public void loginCheck(String account, String password){ if(account.equals("admin")&&password.equals("123")){ Intent intent = new Intent(LoginCheckActivity.this,MainActivity.class); startActivity(intent); LoginCheckActivity.this.finish(); } } }
相关文章推荐
- android 开发中常见问题
- 深入理解Android Gradle
- Android 控件之DatePicker(日期和日历)
- Android Retrofit 框架上传多张图片解决方案
- android增加自定义公用类库customer-framework.jar
- Android自学之路,RecycleView的使用,添加与删除
- Android中static有关的内存泄漏
- Android 打包成jar类库 和 doc文档输出
- Android FloatingActionButton: FloatingActionsMenu向下伸展弹出及删除包含的FloatingActionButton【4】
- 终于能在 Android TV 上看 Twitch 串流了
- Android studio 集成 shareSDK
- android增加自定义公用资源包customer-framework-res.apk
- Android 快速开发系列 打造万能的ListView GridView 适配器
- android SQLite更新数据库版本最佳写法
- Android性能优化之被忽视的优化点
- android studio 签名realse版本apk遇到的问题
- EditText在Dialog中得到焦点并全部选中且弹出软件键盘
- Android 控件之checkBox
- Android Studio 下载地址与简单配置
- android 去应用商店给软件评分