Android学习之键盘事件
MyKeyDemo.java:
import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.EditText; import android.widget.ImageView; public class MyKeyDemo extends Activity { private EditText input = null; private ImageView img = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.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 { public boolean onKey(View v, int keyCode, KeyEvent event) { switch (event.getAction()) { case KeyEvent.ACTION_UP: String msg = MyKeyDemo.this.input.getText().toString(); // 取得输入的文字信息 if (msg.matches("\\w+@\\w+\\.\\w+")) { // 验证通过 MyKeyDemo.this.img.setImageResource(R.drawable.right); // 设置正确图片 } else { MyKeyDemo.this.img.setImageResource(R.drawable.wrong); // 设置错误图片 } case KeyEvent.ACTION_DOWN: // 键盘按下 break; } return false; } } }
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请输入EMAIL地址:" /> <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>
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28