书接上文,我们在登陆界面中编辑了注册按钮,进入当前的功能模块
这是一个简单的XML文件代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/backgrongd"
android:orientation="vertical"
tools:context=".login.Register">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:background="@drawable/border_off">
<TextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="用户账号"
android:textSize="24dp" />
<EditText
android:id="@+id/register_edt_username"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:hint="请输入账号" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/border_off">
<TextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="用户性别"
android:textSize="24dp" />
<EditText
android:id="@+id/register_edt_sex"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:hint="请输入用户性别" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/border_off">
<TextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="用户密码"
android:textSize="24dp" />
<EditText
android:id="@+id/register_edt_password"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/border_off">
<TextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="再次输入密码"
android:textSize="24dp" />
<EditText
android:id="@+id/register_edt_repwd"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:hint="请确认密码"
android:inputType="textPassword" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/border_off">
<TextView
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="用户电话"
android:textSize="24dp" />
<EditText
android:id="@+id/register_edt_phonenumber"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:hint="请输入用户电话" />
</LinearLayout>
<Button
android:id="@+id/but_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/border_selector"
android:text="注册" />
</LinearLayout>
功能模块
private void inDatas() {
butRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
userName = registerEdtUsername.getText().toString().trim();
password = registerEdtPassword.getText().toString().trim();
sex = registerEdtSex.getText().toString().trim();
repwd = registerEdtRepwd.getText().toString().trim();
if (userName.equals("") || nickName.equals("") || password.equals("") || repwd.equals("") || phonenumber.equals("") || sex.equals("")) {
Toast.makeText(Register.this, "内容不能为空!", Toast.LENGTH_SHORT).show();
} else {
if (password.equals(repwd)) {
Toast.makeText(Register.this, "两次密码不同", Toast.LENGTH_SHORT).show();
} else {
if (sex.equals("男")) {
sex = "0";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("userName", userName);
jsonObject.put("password", repwd);
jsonObject.put("phonenumber", phonenumber);
jsonObject.put("sex", sex);
} catch (JSONException e) {
e.printStackTrace();
}
RequestQueue requestQueue = Volley.newRequestQueue(Register.this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://124.93.196.45:10091/prod-api/api/register", jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
if (jsonObject.getString("msg").contains("操作成功")) {
Toast.makeText(getApplicationContext(), "" + jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
startActivity(new Intent(Register.this, Login.class));
} else {
Toast.makeText(getApplicationContext(), "" + jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(Register.this, "网络请求错误", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest);
} else if (sex.equals("女")) {
sex = "1";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("userName", userName);
jsonObject.put("password", repwd);
jsonObject.put("phonenumber", phonenumber);
jsonObject.put("sex", sex);
} catch (JSONException e) {
e.printStackTrace();
}
RequestQueue requestQueue = Volley.newRequestQueue(Register.this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
if (jsonObject.getString("msg").contains("操作成功")) {
Toast.makeText(getApplicationContext(), "" + jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
startActivity(new Intent(Register.this, Login.class));
} else {
Toast.makeText(getApplicationContext(), "" + jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(Register.this, "网络请求错误", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest);
} else {
Toast.makeText(Register.this, "性别请输入男/女", Toast.LENGTH_SHORT).show();
}
}
}
}
});
}