代码仅供参考
Java
package com.example.login_register;
import static baseinfo.Connectinfo.Registerurl;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class Register extends AppCompatActivity {
Button button;
TextView textView;
EditText et1,et2,et3,et4,et5,et6,et7;
String w1,w2,w3,w4,w5,w6,w7;
RadioButton radioButton;
RadioGroup radioGroup;
OkHttpClient okHttpClient;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
radioGroup=findViewById(R.id.rg);
button=findViewById(R.id.register);
textView=findViewById(R.id.foot2);
et1=findViewById(R.id.username);
et2=findViewById(R.id.password);
et3=findViewById(R.id.repassword);
et4=findViewById(R.id.nickname);
et5=findViewById(R.id.phone);
et6=findViewById(R.id.email);
et7=findViewById(R.id.IDcard);
handler = new Handler(Looper.getMainLooper());
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent2=new Intent(Register.this,Login.class);
startActivity(intent2);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
w1=et1.getText().toString();
w2=et2.getText().toString();
w3=et5.getText().toString();
w5=et6.getText().toString();
w6=et7.getText().toString();
radioButton=findViewById(radioGroup.getCheckedRadioButtonId());
w4=radioButton.getText().toString();
Map<String,String> params=new HashMap<>();
params.put("userName",w1);
params.put("password",w2);
params.put("phonenumber",w3);
params.put("sex",w4);
params.put("email",w5);
params.put("idCard",w6);
String json=new Gson().toJson(params);
okHttpClient=new OkHttpClient.Builder().build();
RequestBody requestBody=RequestBody.create(MediaType.parse("application/json;charset=utf-8"),json);
Request request=new Request.Builder()
.post(requestBody)
.url(Registerurl)
.build();
Call call=okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String data=response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
if (response!=null&&response.isSuccessful()){
JSONObject jsonObject=null;
try {
jsonObject=new JSONObject(data);
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonObject.optString("code").equals("200")){
Intent intent1=new Intent(Register.this,Login.class);
startActivity(intent1);
}else {
Toast.makeText(Register.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
}
}
}
});
}
});
}
});
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:background="#2196F3"
android:text="注册"
android:textSize="30dp"
android:textColor="@color/white"
android:gravity="center"
/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/edittext"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:hint="请输入用户名"
/>
<EditText
android:id="@+id/password"
android:password="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/edittext"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:hint="请输入密码"
/>
<EditText
android:id="@+id/repassword"
android:password="true"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/edittext"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:hint="请输入再次密码"
/>
<EditText
android:id="@+id/nickname"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/edittext"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:hint="请输入昵称"
/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/edittext"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:hint="请输入手机号"
/>
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/edittext"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:hint="请输入邮箱"
/>
<EditText
android:id="@+id/IDcard"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/edittext"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:hint="请输入身份证"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:gravity="center"
android:text="性别:"
android:textSize="20dp" />
<RadioGroup
android:id="@+id/rg"
android:layout_toRightOf="@+id/tv"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:text="男"/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:gravity="center"
android:text="女"/>
</RadioGroup>
</RelativeLayout>
<Button
android:id="@+id/register"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#2196F3"
android:layout_marginTop="40dp"
android:layout_gravity="center"
android:text="注册"
android:textSize="20dp"
android:textColor="@color/white"
/>
<TextView
android:id="@+id/foot2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="已有帐号,返回登陆"
android:textSize="20dp"
android:textColor="#131314"
android:layout_gravity="center"
android:layout_marginTop="40dp"
/>
</LinearLayout>