首先 是一个封装好的工具类 里面四种 ,如果需求不一样可以自己 在里面写方法封装
public class RegexUtils {
private boolean startCheck(String reg, String string) {
boolean tem = false;
Pattern pattern = Pattern.compile(reg);
Matcher matcher = pattern.matcher(string);
tem = matcher.matches();
return tem;
}
/**
* 判断手机号是否正确
*
* @param phone
* @return
*/
public static boolean checkPhone(String phone) {
String telRegex = "^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}$";
return phone.matches(telRegex);
}
/**
* 验证密码格式
* 6-16位 大小写特殊字符,无划线,横线等
* @param password
* @return
*/
public static boolean checkPassword(String password) {
String psRegex = "^(?=.*?[a-zA-Z])(?=.*?[0-9])[a-zA-Z0-9]{6,16}$";
return password.matches(psRegex);
}
/**
* 验证用户名
*/
/**
* 检验用户名 取值范围为a-z,A-Z,0-9,"_",汉字,不能以"_"结尾 用户名有最小长度和最大长度限制,比如用户名必须是4-20位
* */
public boolean checkUsername(String username, int min, int max) {
String regex = "[\\w\u4e00-\u9fa5]{" + min + "," + max + "}(?<!_)";
return startCheck(regex, username);
}
/**
* 检验空白符
* */
public boolean checkWhiteLine(String line) {
String regex = "(\\s|\\t|\\r)+";
return startCheck(regex, line);
}
}
里面已经标注好了 那些方法
下面举例一个登陆
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.lbb.rosewoodclient.TestsActivity">
<TextView
android:textSize="20dp"
android:text="账号 :"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/zhanghao"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/denglu"
android:text="登录"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
下面就是对应的Activity
package com.lbb.rosewoodclient;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.lbb.rosewoodclient.utils.RegexUtils;
public class TestsActivity extends AppCompatActivity {
EditText zhanghao;
Button denglu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tests);
zhanghao = (EditText)findViewById(R.id.zhanghao);
denglu = (Button)findViewById(R.id.denglu);
denglu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String ss = zhanghao.getText().toString();
if(ss!=null&&ss.length()>=11)
{
//这里面调用 工具类里面 手机号的方法
boolean checkPhone = RegexUtils.checkPhone(ss.toString());
if(checkPhone)
{
Toast.makeText(TestsActivity.this,"登录成功",Toast.LENGTH_LONG).show();
}else if(!checkPhone)
{
Toast.makeText(TestsActivity.this,"格式失败",Toast.LENGTH_LONG).show();
}
}
}
});
}
}