ty extends Activity {
EditText etUserName,etUserPass;
CheckBox chk;
SharedPreferences pref;
Editor editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定对应布局控件
etUserName=(EditText)findViewById(R.id.etuserName);
etUserPass=(EditText)findViewById(R.id.etuserPass);
chk=(CheckBox)findViewById(R.id.checkBox1);
pref=getSharedPreferences("UserInfo",MODE_PRIVATE);
editor=pref.edit();
String name=pref.getString("userName", "");
if(name==null){
chk.setChecked(false);
}else{
chk.setChecked(true);
etUserName.setText(name);
}
}
//为按钮添加响应
public void doClick(View v){
switch(v.getId()){
case R.id.btnLogin:
//转成字符串进行判断
String name=etUserName.getText().toString().trim();
String pass=etUserPass.getText().toString().trim();
if("admin".equals(name)&&"123456".equals(pass)){
if(chk.isChecked()){
//用户名与密码均正确且保存用户名确认框处于选,
//则保存数据并提交到数据库
editor.putString("userName", name);
editor.commit();
}else{
editor.remove("userName");
editor.commit();
}
//加信息提示框
Toast.makeText(MainActivity.this, "登录成功",
Toast.LENGTH_LONG).show();
}else{Toast.makeText(MainActivity.this, "禁止登录",
Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
}
|