android键盘事件

 

ActivityEvent.java

 

package archie.android.activity; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.Toast; public class ActivityEvent extends Activity { private Button ok; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ok=(Button) findViewById(R.id.myButton); ok.setText(R.string.ok); ok.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { DisplayToast("点击了OK按钮"); } }); } /*键按下事件*/ public boolean onKeyDown(int keyCode,KeyEvent event){ switch(keyCode){ case KeyEvent.KEYCODE_DPAD_CENTER: DisplayToast("按下:中键"); break; case KeyEvent.KEYCODE_DPAD_UP: DisplayToast("按下:上方向键"); break; case KeyEvent.KEYCODE_DPAD_DOWN: DisplayToast("按下:下方向键"); break; case KeyEvent.KEYCODE_DPAD_LEFT: DisplayToast("按下:左方向键"); break; case KeyEvent.KEYCODE_DPAD_RIGHT: DisplayToast("按下:右方向键"); break; } return super.onKeyDown(keyCode, event); } /*释放按键事件*/ public boolean onKeyUp(int keyCode,KeyEvent event){ switch(keyCode){ case KeyEvent.KEYCODE_DPAD_CENTER: DisplayToast("释放:中键"); break; case KeyEvent.KEYCODE_DPAD_UP: DisplayToast("释放:上方向键"); break; case KeyEvent.KEYCODE_DPAD_DOWN: DisplayToast("释放:下方向键"); break; case KeyEvent.KEYCODE_DPAD_LEFT: DisplayToast("释放:左方向键"); break; case KeyEvent.KEYCODE_DPAD_RIGHT: DisplayToast("释放:右方向键"); break; } return super.onKeyUp(keyCode, event); } /*连击事件*/ public boolean onKeyMultiple(int keyCode,int repeatCount,KeyEvent event){ return super.onKeyMultiple(keyCode, repeatCount, event); } /*触笔事件*/ public boolean onTouchEvent(MotionEvent event){ int iAction=event.getAction(); if(iAction==MotionEvent.ACTION_CANCEL||iAction==MotionEvent.ACTION_DOWN|| iAction==MotionEvent.ACTION_MOVE){ return false; } // int x=(int)event.getX(); int y=(int) event.getY(); DisplayToast("触笔点击坐标:("+Integer.toString(x)+","+Integer.toString(y)+")"); return super.onTouchEvent(event); } private void DisplayToast(String str) { Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); } }

 

你可能感兴趣的:(android键盘事件)