需要一个点击事件就是监听
but_post.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PHONE();//验证电话 PASSWORD();//验证密码 } });
在验证前创建一个正则表达式
public class Utils { public static boolean isPhone(String inputText) { Pattern pat = Pattern.compile("^((14[0-9])|(13[0-9])|(15[0-9])|(18[0-9])|(17[0-9]))\\d{8}$"); Matcher mat = pat.matcher(inputText); return mat.matches(); }
它是用于验证消息
验证电话的代码如下:
private void PHONE() { String userphone = phone.getText().toString().trim(); if (TextUtils.isEmpty(userphone)) { Toast.makeText(MainActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show(); return; } boolean result = Utils.isPhone(userphone); if (result == false) { Toast.makeText(MainActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); return; } else { if (result == false) { Toast.makeText(MainActivity.this, "完成注册", Toast.LENGTH_SHORT).show(); } } }
密码如下:
private void PASSWORD() { if (password.length()>6){ Toast.makeText(MainActivity.this, "密码不可超过6位", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(MainActivity.this, "ojbk", Toast.LENGTH_SHORT).show(); } }