OnKeyListener(键盘事件)
正则表达式 w:任意字符 +:一个或一个以上字符
1 TextView tv=(TextView) findViewById(R.id.keyTV02); 2 EditText input=(EditText) findViewById(R.id.keyET01); 3 ImageView img=(ImageView) findViewById(R.id.keyImg01); 4 input.setOnKeyListener(new OnKeyListener(){ 5 public boolean onKey(View v, int keyCode, KeyEvent event) { 6 switch(event.getAction()){ 7 case KeyEvent.ACTION_UP: //键盘松开触发 8 String str=input.getText().toString(); 9 if(str.matches("\w+@\w+\.\w+")){ //判断是否是Email 10 img.setImageResource(R.drawable.图片名称); 11 tv.setVisibility (View. INVISIBLE); //设置文字显示或隐藏 12 }else{ 13 img.setImageResource(R.drawable.图片名称); 14 tv.setVisibility (View.VISIBLE); 15 } 16 case KeyEvent.ACTION_DOWN: //键盘按下触发 17 default: 18 break; 19 } 20 return false; 21 } );
OnTouchListener(触摸事件)
1 protected void onCreate(Bundle savedInstanceState) { 2 super.onCreate(savedInstanceState); 3 setContentView(R.layout.touch); 4 ImageView iv=(ImageView)findViewById(R.id.ImgV); 5 TextView tv=(TextView)findViewById(R.id.TV); 6 iv.setOnTouchListener(new OnTouchListener() { 7 public boolean onTouch(View v, MotionEvent event) { 8 String str = event.getX()+","+event.getY(); 9 tv.setText(str); 10 return false; 11 } 12 }); 13 }