setOnKeyListener---邮箱验证

运行效果图



布局文件activity_main.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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=".MainActivity" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Email格式的检测" />

   <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
	    <EditText
	        android:id="@+id/etInfo"
	        android:layout_width="match_parent"
	        android:layout_height="match_parent"
	        android:layout_weight="1"
	        android:ems="10" 
	        android:hint="请输入Email"/>
		<ImageView 
		    android:id="@+id/IvShow"
		    android:layout_width="match_parent"
		    android:layout_height="match_parent"
		    android:layout_weight="4"/>
    </LinearLayout>
    

</LinearLayout>

修改MainActivity.java

需要两个图片来显示验证是否通过,wrong_bg.png和right_bg.png


import java.util.Timer;
import java.util.TimerTask;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.text.method.KeyListener;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private EditText etInfo=null;//输入框
	private ImageView IvShow=null;//判断显示图
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		findById();
		setLisitener();//设置视图的监听对象
	}
	private void setLisitener() {
		//设置输入框组件绑定的监听事件
		etInfo.setOnKeyListener(new View.OnKeyListener() {
			@Override
			public boolean onKey(View view, int keyCode, KeyEvent event) {
				switch (event.getAction()) {//得到操作类型
				case KeyEvent.ACTION_DOWN://键盘按下
					String inputString=etInfo.getText().toString();//获取输入框中的额内容
					if(inputString.matches("\\w+@\\w+\\.\\w+")){//利用正则表达式进行验证
						IvShow.setImageResource(R.drawable.right_bg);//通过
					}else{
						IvShow.setImageResource(R.drawable.wrong_bg);//不通过
					}
					break;
				case KeyEvent.ACTION_UP://键盘弹起
					
					break;
				}
				return false;
			}
		});
	}
	
	private void findById() {
		etInfo=(EditText) findViewById(R.id.etInfo);
		IvShow=(ImageView) findViewById(R.id.IvShow);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

饕餮幻想家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值