效果图
1.MainActivity.java
package com.example.app2; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends AppCompatActivity {
private TextView textView;
private EditText editText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.tv_show);
editText = (EditText) findViewById(R.id.et_mail1); editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
String et_info = editText.getText().toString();
String reges="\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}"; //正则表达式验证邮箱是否正确
if(et_info.matches(reges)){
textView.setText("您输入的邮箱是:"+et_info+"符合条件");
}else{
textView.setText("您输入的邮箱是:"+et_info+"不符合条件");
}
}
}); }
}
2.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="com.example.app2.MainActivity"
android:orientation="vertical"
> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入您的邮箱" />
<EditText
android:id="@+id/et_mail1"
android:hint="第一个文本输入框"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/et_mail2"
android:hint="第二个文本输入框"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>