一、Xml布局中
<<span style="color:#000080;font-weight:bold;">EditText android:id="@+id/mEditText1" android:hint="请输入手机号或邮箱" android:layout_width="match_parent" android:layout_height="wrap_content" /> <<span style="color:#000080;font-weight:bold;">EditText android:id="@+id/mEditText2" android:hint="请输入密码" android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> <<span style="color:#000080;font-weight:bold;">Button android:id="@+id/mBtn1" android:text="登录" android:layout_marginTop="50dp" android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" />
二、Activity中
private void submit() { // validate String mEditText1String = mEditText1.getText().toString().trim(); if (TextUtils.isEmpty(mEditText1String)) { Toast.makeText(this, "请输入手机号或邮箱", Toast.LENGTH_SHORT).show(); return; } String mEditText2String = mEditText2.getText().toString().trim(); if (TextUtils.isEmpty(mEditText2String)) { Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show(); return; } String regEx2= "[0-9a-zA-Z]{6,16}"; String regEx = "^(\\d{11})$"; Pattern pattern = Pattern.compile(regEx2); Matcher matcher = pattern.matcher(mEditText2String); if (matcher.find()){ Toast.makeText(this, "密码正确", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(this, "请输入正确密码", Toast.LENGTH_SHORT).show(); return; } // TODO validate success, do something }
三、另一种方式//手机号码
Pattern compile = Pattern.compile("^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$");
Matcher matcher = compile.matcher(mEditText1.getText().toString());
boolean b = matcher.find();
//密码(以字母开头,长度在6~18之间,只能包含字母、数字和下划线)
Pattern compile1 = Pattern.compile("^[a-zA-Z]\\w{5,17}$");
Matcher matcher1 = compile1.matcher(mEditText2.getText().toString());
boolean b2 = matcher1.find();
if (b&&b2&&mEditText1.getText().toString().equals(aa)){
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}else if(b&&b2&&mEditText1.getText().toString().equals(bb)){
Intent intent1 = new Intent(MainActivity.this, MainActivity3.class);
startActivity(intent1);
}else {
Toast.makeText(this, "您输入的账号密码不正确,请重新输入", Toast.LENGTH_SHORT).show();
}