我看博客有个习惯,就是先放置图片在前面的时候就会一直看下去,那我也把我的博客先加上图片吧,呵呵。
写了个登录的注册,关键技术用SharedPreferences存放对象,可以存放用户对象的信息。
注册界面的重置会重置回原先的状态,单击提交,我们点击登录。
进入下面的界面:
点击登陆,界面显示为
,是的这里用SharedPreferences存储了你刚刚注册的对象
下面就展示一下关键代码。。。
1.重置按钮部分:
registerusername.setText("");
regisrteruserpassword.setText("");
radioGroupmen.setChecked(true);
box1.setChecked(false);
box2.setChecked(false);
box3.setChecked(false);
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int mounth = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
datePicker.init(year, mounth, day, null); //对DatePicker的重置,不用添加监听,所以设置成null
2.提交按钮部分:
if (registerusername.getText().toString().equals("")) {
Toast.makeText(Register.this, "请输入你的账号", Toast.LENGTH_SHORT).show();
return;
}else if (regisrteruserpassword.getText().toString().equals("")) {
Toast.makeText(Register.this, "请输入你的密码", Toast.LENGTH_SHORT).show();
return;
}
Vip vip = new Vip();
vip.setName(registerusername.getText().toString());
vip.setPassword(regisrteruserpassword.getText().toString());
if(radioGroupmen.isChecked()){
vip.setSix("男");
}
if(radioGroupwomen.isChecked()){
vip.setSix("女");
}
vip.setBrithday(datePicker.getYear()+"年"+(datePicker.getMonth()+1)+"月"+datePicker.getDayOfMonth()+"日");
StringBuffer stringBuffer = new StringBuffer();
if(box1.isChecked()){
stringBuffer.append("游泳");
}
if(box2.isChecked()){
stringBuffer.append("看书");
}
if (box3.isChecked()) {
stringBuffer.append("打篮球");
}
vip.setInterest(stringBuffer.toString());
SharedPreferences preferences = getSharedPreferences("Vip", 0);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(vip);
String productBase64 = new String(Base64.encodeBase64(byteArrayOutputStream
.toByteArray()));
Editor editor = preferences.edit();
editor.putString(vip.getName(), productBase64);
editor.commit();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent(Register.this , MainActivity.class);
Register.this.finish();
startActivity(intent);
3.登陆验证部分:
Vip vip = null;
SharedPreferences preferences = getSharedPreferences("Vip", MODE_PRIVATE);
String productBase64 = preferences.getString(username.getText().toString(), "");
if (!productBase64.equals("")) {
byte[] base64 = Base64.decodeBase64(productBase64.getBytes());
ByteArrayInputStream bais = new ByteArrayInputStream(base64);
try {
ObjectInputStream bis = new ObjectInputStream(bais);
try {
vip = (Vip) bis.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (vip.getPassword().equals(userpassword.getText().toString())) {
Intent intent = new Intent(Login.this,Success.class);
intent.putExtra("Vip", vip);
startActivity(intent);
}else {
Toast.makeText(Login.this, "密码不正确,请重新输入 ", Toast.LENGTH_SHORT).show();
return;
}
}else {
Toast.makeText(Login.this, "账号输入错误", Toast.LENGTH_SHORT).show();
return;
} 其他的就不展示了,在资源里面我上传了。