a-5
注册界面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_register" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:background="@drawable/d" tools:context=".register"> <TextView android:id="@+id/login_title" android:text="注册" android:layout_width="match_parent" android:textSize="25sp" android:textColor="@color/blue" android:layout_height="50dp" android:gravity="center" /> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/login_title" > <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"> <TextView android:text="账号:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" /> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:inputType="phone" android:layout_weight="1" android:textSize="20sp" android:ems="10" android:id="@+id/et_ID" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"> <TextView android:text="密码:" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" /> <EditText android:layout_width="0dp" android:layout_height="wrap_content" android:textSize="20sp" android:inputType="textPassword" android:ems="10" android:layout_weight="1" android:id="@+id/et_password" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"> <Button android:text="注册" android:textColor="@color/white" android:layout_weight="1" android:textSize="30sp" android:layout_gravity="center" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/bt_register" android:background="@drawable/button_shape"/> </TableRow> </TableLayout> </RelativeLayout>
package com.example.hhh.telephone; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public class register extends AppCompatActivity implements View.OnClickListener { private Button bt_register; private EditText et_ID,et_password; private String username,password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); et_ID = (EditText) findViewById(R.id.et_ID); et_password = (EditText) findViewById(R.id.et_password); bt_register = (Button) findViewById(R.id.bt_register); bt_register.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.bt_register: username = et_ID.getText().toString().trim(); password = et_password.getText().toString().trim(); if(username.equals("") || password.equals("")){ Toast.makeText(this,"请完善信息!",Toast.LENGTH_SHORT).show(); return; }else { userService userService = new userService(this); Boolean flag = userService.CheckIsDataAlreadyInDBorNot(username); if(flag==Boolean.TRUE){ Toast.makeText(this,"该用户已存在!",Toast.LENGTH_SHORT).show(); return; }else { user user = new user(); user.setUsername(username); user.setPassword(password); userService.register(user); Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show(); finish(); } } break; } } }