大家好,第一次写博客,难免有很多不足,希望大家指出来,共同进步。
废话不多说,直接上代码,布局那些不就写了,太简单了。就是两个Edittext:账号和密码,两个Checkbox:记住密码和显示密码;
先来显示密码的代码:
ck_xian.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked){ ed_pass.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); }else { ed_pass.setTransformationMethod(PasswordTransformationMethod.getInstance()); } } });
接下来是记住密码的代码:
String aa = ed_name.getText().toString(); SharedPreferences sharedPreferences = getSharedPreferences("ss",MODE_PRIVATE); if (aa==null||aa.equals("")){ ed_name.setText(sharedPreferences.getString("name","")); ed_pass.setText(sharedPreferences.getString("pass","")); ck_pass.setChecked(true); }else { ck_pass.setChecked(false); } ck_pass.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { SharedPreferences.Editor editor = getSharedPreferences("ss",MODE_PRIVATE).edit(); if (isChecked){ editor.putString("name",ed_name.getText().toString()); editor.putString("pass",ed_pass.getText().toString()); editor.commit(); }else { editor.clear(); } } });