MyDemo.java
package com.jackie.keyproject;
import android.app.Activity;import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.ImageView;
public class MainActivity extends Activity {
private EditText input=null;
private ImageView img=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.input=(EditText)super.findViewById(R.id.input);
this.img=(ImageView)super.findViewById(R.id.img);
this.input.setOnKeyListener(new OnKeyListenerImpl());
}
private class OnKeyListenerImpl implements OnKeyListener{
@Override
public boolean onKey(View arg0, int arg1, KeyEvent event) {
switch(event.getAction()){
case KeyEvent.ACTION_UP:
String msg=MainActivity.this.input.getText().toString();
if(msg.matches("\\w+@\\w+\\.\\w+")){
MainActivity.this.img.setImageResource(R.drawable.right);
}else{
MainActivity.this.img.setImageResource(R.drawable.wrong);
}
case KeyEvent.ACTION_DOWN:
break;
}
return false;
}}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Main.xml
<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:orientation="horizontal"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入EMALL地址:" />
<EditText
android:id="@+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"/>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/wrong"/>
</LinearLayout>