【基于Java语言的Android个人开发笔记,个人工具类笔记】注册内容

书接上文,我们在登陆界面中编辑了注册按钮,进入当前的功能模块
这是一个简单的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();
                        }
                    }
                }
            }
        });
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值