android:layout_weight=“1”
android:ems=“10”
android:gravity=“center”
android:inputType=“textPassword”
android:hint=“请输入密码”
/>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:textSize=“15sp”
android:gravity=“center”
android:text=“邮箱:” />
<EditText
android:id=“@+id/email”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:ems=“10”
android:gravity=“center”
android:inputType=“textEmailAddress”
android:hint=“请输入邮箱”
/>
<LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”>
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:textSize=“15sp”
android:gravity=“center”
android:text=“手机号:” />
<EditText
android:id=“@+id/phone”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:ems=“10”
android:gravity=“center”
android:inputType=“p
hone”
android:maxLength=“11”
android:hint=“请输入手机号”
/>
<Button
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:textSize=“25sp”
android:onClick=“register”
android:text=“注册” />
<Button
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:textSize=“25sp”
android:onClick=“reset”
android:text=“重置” />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.application22;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends AppCompatActivity {
EditText name = null;
EditText password = null;
EditText email = null;
EditText phone = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
password = findViewById(R.id.password);
email = findViewById(R.id.email);
phone = findViewById(R.id.phone);
}
public void register(View v){
String namestring = name.getText().toString();
String passwordstring = password.getText().toString();
String emailstring = email.getText().toString();
String phonestring = phone.getText().toString();
//判断账号
if(namestring.length() == 0 ){
Toast.makeText(getApplicationContext(),“账号不能为空”,Toast.LENGTH_SHORT).show();
return;
}
if(namestring.length() > 16 ){
Toast.makeText(getApplicationContext(),“账号必须小于16位”,Toast.LENGTH_SHORT).show();
return;
}
if(namestring.length() != 0 && namestring.length() < 8 ){
Toast.makeText(getApplicationContext(),“账号必须大于8位”,Toast.LENGTH_SHORT).show();
return;
}
//判断密码
if(passwordstring.length() == 0 ){
Toast.makeText(getApplicationContext(),“密码不能为空”,Toast.LENGTH_SHORT).show();
return;
}
if(passwordstring.length() > 16 ){
Toast.makeText(getApplicationContext(),“密码必须小于16位”,Toast.LENGTH_SHORT).show();
return;
}
if(passwordstring.length() != 0 && passwordstring.length() < 8 ){
Toast.makeText(getApplicationContext(),“密码必须大于8位”,Toast.LENGTH_SHORT).show();
return;
}
//判断邮箱
if(emailstring.length() == 0 ){
Toast.makeText(getApplicationContext(),“邮箱不能为空”,Toast.LENGTH_SHORT).show();
return;
}
String regEx1 = “^([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$”;
Pattern p;
Matcher m;
p = Pattern.compile(regEx1);
m = p.matcher(emailstring);
if (!m.matches()){
Toast.makeText(getApplicationContext(),“邮箱格式不正确”,Toast.LENGTH_SHORT).show();
return;
}
//判断电话
if(phonestring.length() == 0 ){
Toast.makeText(getApplicationContext(),“电话不能为空”,Toast.LENGTH_SHORT).show();
return;
}
Pattern p1 = Pattern.compile(“^1[3,5,7,8,9][0-9]{9}$”);
Matcher m1 = p1.matcher(phonestring);
if(!m1.matches()){
Toast.makeText(getApplicationContext(),“电话格式不正确”,Toast.LENGTH_SHORT).show();
return;
}
Toast.makeText(getApplicationContext(),“注册成功请登录”,Toast.LENGTH_SHORT).show();
}
public void reset(View v){