效果图
1.MainActivity.java
1 package com.example.app2; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.EditText; 7 import android.widget.TextView; 8 9 public class MainActivity extends AppCompatActivity { 10 private TextView textView; 11 private EditText editText; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 textView = (TextView) findViewById(R.id.tv_show); 19 editText = (EditText) findViewById(R.id.et_mail1); 20 21 editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 22 @Override 23 public void onFocusChange(View v, boolean hasFocus) { 24 String et_info = editText.getText().toString(); 25 String reges="\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}"; //正则表达式验证邮箱是否正确 26 if(et_info.matches(reges)){ 27 textView.setText("您输入的邮箱是:"+et_info+"符合条件"); 28 }else{ 29 textView.setText("您输入的邮箱是:"+et_info+"不符合条件"); 30 } 31 } 32 }); 33 34 } 35 }
2.activity_main.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.app2.MainActivity" 11 android:orientation="vertical" 12 > 13 14 <TextView 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:text="请输入您的邮箱" /> 18 <EditText 19 android:id="@+id/et_mail1" 20 android:hint="第一个文本输入框" 21 android:layout_width="match_parent" 22 android:layout_height="wrap_content" /> 23 <EditText 24 android:id="@+id/et_mail2" 25 android:hint="第二个文本输入框" 26 android:layout_width="match_parent" 27 android:layout_height="wrap_content" /> 28 <TextView 29 android:id="@+id/tv_show" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" /> 32 </LinearLayout>