本文主要是介绍登录中用于记住用户名和密码的方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
public abstract class ADtLoginActivity extends ADtBaseActivity{
private EditText mTextUserName;
private EditText mTextPassWord;
private CheckBox mCheckBox;
private SharedPreferences mSharedPreferences;
//保存记住的用户名和密码
String saveUserName;
String savePassWord;
Editor editor;
@Override
protected int getLayoutId()
{
return R.layout.login;
}
@Override
protected void onLoadView()
{
mTextUserName = findEditTextById(R.id.edit_txt_user_name);
mTextPassWord = findEditTextById(R.id.edit_txt_password);
}
@Override
protected void onLoadContent()
{
((ImageButton) findViewById(R.id.btn_title_left)).setImageResource(R.drawable.btn_back);
((ImageButton) findViewById(R.id.btn_title_left)).setOnClickListener(this);
findViewById(R.id.btn_title_right).setVisibility(View.INVISIBLE);
((TextView) findViewById(R.id.txt_title)).setText("登陆");
mCheckBox=(CheckBox)findViewById(R.id.cb_remember_password);
mSharedPreferences=getSharedPreferences("remember_password",MODE_PRIVATE);
saveUserName=mSharedPreferences.getString("username", null);
savePassWord=mSharedPreferences.getString("password",null);
if (savePassWord!=null&&saveUserName!=null) {
mCheckBox.setChecked(true);
mTextUserName.setText(saveUserName);
mTextPassWord.setText(savePassWord);
}
}
@Override
protected void onLoadBind()
{
findButtonById(R.id.btn_register).setOnClickListener(this);
findButtonById(R.id.btn_login).setOnClickListener(this);
findButtonById(R.id.btn_reset).setOnClickListener(this);
findViewById(R.id.btn_qq_login_on).setOnClickListener(this);
findViewById(R.id.btn_sina_login_on).setOnClickListener(this);
}
@Override
protected void onClick(View v, int id)
{
if (id == R.id.btn_title_left)
{
this.finish();
} else if (id == R.id.btn_register)
{
onBtnRegister();
} else if (id == R.id.btn_login)
{
//1、获取SharePrefernce里面保存的用户名和密码
editor = mSharedPreferences.edit();
if (mCheckBox.isChecked())
{
//2、保存用户名和密码在shareprerence
editor.putString("username", mTextUserName.getText().toString().trim());
editor.putString("password", mTextPassWord.getText().toString().trim());
editor.commit();
onBtnLogin(mTextUserName.getText().toString().trim(), mTextPassWord.getText().toString().trim());
mCheckBox.setChecked(true);
}else if (savePassWord!=null&&savePassWord!=null){
//editor=mSharedPreferences.edit();
editor.putString("username",null).commit();
editor.putString("password",null).commit();
mCheckBox.setChecked(false);
}
onBtnLogin(mTextUserName.getText().toString().trim(), mTextPassWord.getText().toString().trim());
} else if (id == R.id.btn_reset)
{
onBtnResetPassword();
} else if (id == R.id.btn_sina_login_on)
{
onSinaLogin();
} else if (id==R.id.btn_qq_login_on);
{
onQQLogin();
}
}
protected abstract void onBtnRegister();
protected abstract void onBtnLogin(String userName, String passWord);
protected abstract void onBtnResetPassword();
protected abstract void onQQLogin();
protected abstract void onSinaLogin();
这篇关于登录中用于记住用户名和密码的方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!