我们代码的目的很简单,那就是监听上下左右中这几个键的事件触发。直接上代码:
dpad.xml
- <?xml version="1.0" encoding="utf-8"?>
- <AbsoluteLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#FFFFFF"
- >
- <Button
- android:id="@+id/myButton1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="move me"
- android:layout_x="20px"
- android:layout_y="40px" />
- </AbsoluteLayout>
activity代码:
- package cn.com.chenzheng_java;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.DisplayMetrics;
- import android.view.KeyEvent;
- import android.widget.AbsoluteLayout;
- import android.widget.Button;
- import android.widget.Toast;
-
-
-
-
-
- public class DpadActivity extends Activity {
- Button button;
- DisplayMetrics metrics = new DisplayMetrics();
- int screenx = 0 ;
- int screeny = 0 ;
- int buttonWidth = 80;
- int buttonHeight = 40 ;
- int currentX = 0;
- int currentY = 0;
- int step = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.dpad);
- button = (Button) findViewById(R.id.myButton1);
- getWindowManager().getDefaultDisplay().getMetrics(metrics);
- screenx = metrics.widthPixels;
- screeny = metrics.heightPixels;
-
-
-
- currentX = (screenx-buttonWidth)/2;
- currentY = (screeny-buttonHeight)/2;
- step = 2;
-
- button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY));
-
- }
-
-
-
-
-
-
-
-
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- switch (keyCode) {
- case KeyEvent.KEYCODE_DPAD_DOWN:
- moveDown();
- break;
- case KeyEvent.KEYCODE_DPAD_UP:
- moveUp();
- case KeyEvent.KEYCODE_DPAD_LEFT:
- moveLeft();
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- moveRight();
- default:
- break;
- }
-
- return super.onKeyDown(keyCode, event);
- }
-
-
- @SuppressWarnings("deprecation")
- private void moveDown(){
- int temp = currentY+step;
- if(temp>(screeny-buttonHeight)){
- showToast("到头了哦!");
- button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, screenx, screeny-buttonHeight));
- }
- else{
- currentY = currentY+step;
- AbsoluteLayout.LayoutParams params =
- new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY);
- button.setLayoutParams(params);
-
- }
-
-
-
- }
-
- @SuppressWarnings("deprecation")
- private void moveUp(){
- int temp = currentY-step;
- if(temp<=0){
- showToast("往上到头了哦!");
- button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, screenx, 0));
- }
- else{
- currentY = currentY-step;
- AbsoluteLayout.LayoutParams params =
- new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY);
- button.setLayoutParams(params);
- }
-
-
- }
-
- @SuppressWarnings("deprecation")
- private void moveLeft(){
- int temp = currentX-step;
- if(temp<=0){
- showToast("往左边到头了哦!");
- button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, 0, screeny));
- }
- else{
- currentX = currentX-step;
- AbsoluteLayout.LayoutParams params =
- new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY);
- button.setLayoutParams(params);
- }
-
-
- }
-
- @SuppressWarnings("deprecation")
- private void moveRight(){
- int temp = currentX+step;
- if(temp>=(screenx-buttonWidth)){
- showToast("往右边到头了哦!");
- button.setLayoutParams(new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, screenx-buttonWidth, currentY));
- }
- else{
- currentX = currentX+step;
- AbsoluteLayout.LayoutParams params =
- new AbsoluteLayout.LayoutParams(buttonWidth, buttonHeight, currentX, currentY);
- button.setLayoutParams(params);
- }
-
-
- }
-
-
-
-
-
- private void showToast(String text){
- Toast.makeText(this, text, Toast.LENGTH_LONG).show();
-
- }
-
- }
---------------------------------------------------------------------------------------
这里我们可以看到,要想监听这几个方向键,那么我们必须重写 public boolean onKeyDown(int keyCode, KeyEvent event)这个方法,该方法定义在Activity中,其中keyCode便是代表着你点击的那个键的标识符,KeyEvent 则是一个事件。
需要注意的是,坐标问题:我们控制的按钮的坐标实际上是左上角的坐标。而屏幕的向左是X轴,向下是Y轴,也就是说,只要在屏幕内显示的组件,其坐标都是正数的。
当然键盘中所有的键都可以监听的,我们亦可以来监听A-Z这些键,他们的keyCode依次是KEYCODE_A -------KEYCODE_Z.
这里有一个小技巧,可以判断出用户输入的是什么A-Z那个字符,代码如下:
- int code = 'A'+keycode-29;
- char ch = (char)code;
如果我们无法获取当前的按键,我们可以通过keycode_unknown来捕捉。
控制音量大小:keycode_volume_down / keycode_volume_up.
-----------------------------------------------------------------------------------------
关于KeyEvent,它代表了我们点击键盘时的事件,看下图,我们可以创建自己的键盘事件
其中参数中的action,代表了用户的操作,值为ACTION_DOWN、ACTION_UP、ACTION_MULTIPLE其中的一个。而code则代表了我们的标识符,入上面提到的keycode_volume_down 就是一个code。