package com.gourmetMeal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.crazyit.auction.client.util.HttpUtil;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;
/**
* 用户注册界面
*
* @author bcu
*
*/
public class RegisterActivity extends CommonActivity implements
OnClickListener, OnCheckedChangeListener {
/**
* 消息传递
*/
private Intent intent;
/**
* 性别的文本
*/
private String string_sex;
/**
* 取消注册按钮
*/
private Button btn_cancel;
/**
* 注册按钮
*/
private Button btn_register;
/**
* 用户名的EditText
*/
private EditText edtxt_username;
/**
* 密码的EditText
*/
private EditText edtxt_password;
/**
* 再次确认密码的EditText
*/
private EditText edtxt_pwdagain;
/**
* 手机号的EditText
*/
private EditText edtxt_phone;
/**
* 选择出生日期的TextView
*/
private TextView txt_date;
/**
* 选择出生日期的底层布局
*/
private LinearLayout linlayout_date;
/**
* 选择性别的RadioGroup
*/
private RadioGroup rad_sex;
/**
* 选择男性的RadioButton
*/
private RadioButton rad_man;
/**
* 选择女性的RadioButton
*/
private RadioButton rad_women;
/**
* 保存用户注册信息的集合
*/
private ArrayList<String> userinfos;
/**
* 服务器返回的结果信息
*/
private String json_back_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
btn_register = (Button) findViewById(R.id.btn_reg_register);
btn_register.setOnClickListener(this);
btn_cancel = (Button) findViewById(R.id.btn_reg_cancel);
btn_cancel.setOnClickListener(this);
edtxt_username = (EditText) findViewById(R.id.edtxt_reg_user);
edtxt_password = (EditText) findViewById(R.id.edtxt_reg_pwd);
edtxt_pwdagain = (EditText) findViewById(R.id.edtxt_reg_pwd2);
edtxt_phone = (EditText) findViewById(R.id.edtxt_reg_phone);
txt_date = (TextView) findViewById(R.id.txt_reg_date);
linlayout_date = (LinearLayout) findViewById(R.id.linlayout_reg_date);
linlayout_date.setOnClickListener(this);
rad_sex = (RadioGroup) findViewById(R.id.rad_reg_sex);
rad_sex.setOnCheckedChangeListener(this);
rad_man = (RadioButton) findViewById(R.id.rad_reg_man);
rad_women = (RadioButton) findViewById(R.id.rad_reg_women);
}
/**
* 点击事件
*/
public void onClick(View v) {
switch (v.getId()) {
// 点击取消,返回登录页面
case R.id.btn_reg_cancel:
intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
// 关闭Activity
this.finish();
// 清空Intent对象
intent = null;
break;
// 点击注册,首先验证信息合法性,错误则弹出对应提示,均正确则弹出确认对话框
case R.id.btn_reg_register:
// 如果用户名为空,则提示
if ("".equals(edtxt_username.getText().toString().trim())) {
Toast.makeText(this, "用户名不可为空!", Toast.LENGTH_SHORT).show();
}
// 如果密码为空,则提示
else if ("".equals(edtxt_password.getText().toString().trim())) {
Toast.makeText(this, "密码不可为空!", Toast.LENGTH_SHORT).show();
}
// 如果两次输入密码不相同,则提示
else if (!edtxt_password.getText().toString().trim()
.equals(edtxt_pwdagain.getText().toString().trim())) {
Toast.makeText(this, "两次输入密码不相同!", Toast.LENGTH_SHORT).show();
}
// 如果性别为空,则提示
else if (string_sex == null) {
Toast.makeText(this, "性别不可为空!", Toast.LENGTH_SHORT).show();
}
// 如果手机号为空,则提示
else if ("".equals(edtxt_phone.getText().toString().trim())) {
Toast.makeText(this, "手机号不可为空!", Toast.LENGTH_SHORT).show();
}
// 如果信息验证都正确,弹出确认注册的对话框
else {
registerDialog();
}
break;
// 点击出生日期,弹出对话框设置
case R.id.linlayout_reg_date:
// 弹出设置出生日期的Dialog
createDateDialog();
break;
default:
break;
}
}
/**
* 用户性别的RadioButton选择事件
*/
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 选择为男性
if (checkedId == rad_man.getId()) {
string_sex = rad_man.getText().toString();
}
// 选择为女性
if (checkedId == rad_women.getId()) {
string_sex = rad_women.getText().toString();
}
}
/**
* 确认注册的对话框
*/
protected void registerDialog() {
// Android中的AlertDialog的创建一般是通过其内嵌类AlertDialog.Builder来实现的
AlertDialog.Builder builder = new Builder(RegisterActivity.this);
// 给对话框设置标题
builder.setTitle("提示");
// 给对话框设置内容
builder.setMessage("确认是否注册用户:" + edtxt_username.getText().toString());
// 给对话框添加”确认”按钮
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 将用户信息添加到集合中
userinfos = new ArrayList<String>();
userinfos.add(edtxt_username.getText().toString().trim());
userinfos.add(edtxt_password.getText().toString().trim());
userinfos.add(string_sex);
userinfos.add(txt_date.getText().toString().trim());
userinfos.add(edtxt_phone.getText().toString().trim() + "");
// 提示注册信息,测试用
Toast.makeText(
getApplicationContext(),
"用户名: " + userinfos.get(0) + "\n密码:" + userinfos.get(1)
+ "\n性别:" + userinfos.get(2) + "\n出生日期:"
+ userinfos.get(3) + "\n手机号:"
+ userinfos.get(4), Toast.LENGTH_LONG).show();
// 关闭对话框
dialog.dismiss();
// 判断是否注册成功
if (registerPro()) {
// 跳转回到登录页面
intent = new Intent(RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
// 关闭Activity
RegisterActivity.this.finish();
// 清空Intent对象
intent = null;
// 显示服务器反馈信息
Toast.makeText(RegisterActivity.this, json_back_message,
Toast.LENGTH_LONG).show();
} else {
// 显示服务器反馈信息
Toast.makeText(RegisterActivity.this, json_back_message,
Toast.LENGTH_LONG).show();
}
}
});
// 给对话框添加”取消”按钮
builder.setNegativeButton("返回", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// 关闭对话框
dialog.dismiss();
}
});
// 创建并显示对话框
builder.create().show();
}
/**
* 设置出生日期的Dialog
*/
public void createDateDialog() {
// 获取当前时间
Calendar calendar = Calendar.getInstance();
// 时间对话框
DatePickerDialog dialog = new DatePickerDialog(
RegisterActivity.this,
new DatePickerDialog.OnDateSetListener() {
/**
* 日期对话框时间设置
*/
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// 获取当前时间
Calendar c = Calendar.getInstance();
// 设置年
c.set(Calendar.YEAR, year);
// 设置月
c.set(Calendar.MONTH, monthOfYear);
// 设置日
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
// 系统自定义输出月份小一月 所以输出MONTH值时强转整形+1
// 判断 如果月日小于10 在前面加0
String strMonth = "";
String strDate = "";
if ((Integer.valueOf(c.get(Calendar.MONTH)) + 1) < 10) {
strMonth = "0"
+ (Integer.valueOf(c.get(Calendar.MONTH)) + 1);
} else {
strMonth = String.valueOf(Integer.valueOf(c
.get(Calendar.MONTH)) + 1);
}
if ((Integer.valueOf(c.get(Calendar.DATE))) < 10) {
strDate = "0"
+ (Integer.valueOf(c.get(Calendar.DATE)));
} else {
strDate = String.valueOf(Integer.valueOf(c
.get(Calendar.DATE)));
}
// 将时间设置到txtDate
txt_date.setText(c.get(Calendar.YEAR) + "-" + strMonth
+ "-" + strDate);
}
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DATE));
// 显示对话框
dialog.show();
}
/**
* 判断是否注册成功
*
* @return
*/
private boolean registerPro() {
JSONObject jsonObj = null;
try {
// 获取返回的JSON对象
jsonObj = query(userinfos);
json_back_message = jsonObj.getString("rtmsg");
// 如果接收到的结果代码"rt"=200
if (jsonObj.getString("rt").equals("200")) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 定义发送请求的方法
*
* @param userinfos
* @return
* @throws Exception
*/
private JSONObject query(ArrayList<String> userinfos) throws Exception {
// 将集合封装到JSONArray对象中
JSONArray array = new JSONArray(userinfos);
// 封装请求参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("list", array.toString()));
// 发送请求
return new JSONObject(HttpUtil.postRequest(HttpUtil.REGISTER_URL,
params));
}
/**
* 重写系统的返回按钮,跳转回登录页面
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
// 关闭Activity
this.finish();
// 清空Intent对象
intent = null;
}
return false;
}
}