文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>DPAD按键处理-onKeyDown与Layout坐标交互

DPAD按键处理-onKeyDown与Layout坐标交互

时间:2010-09-13  来源:fly123456789

此程序继承Activity重写了onKeyDown()方法,传入的参数为keyCode与KeyEvent,前者为按键代码,为整数;后者为KeyEvent对象,常见其构建的方法为KeyEvent(int action,int code).
keyCode与KeyEvent.KEYCODE_DPAD_CENTER(居中)、KeyEvent.KEYCODE_DPAD_DOWN(向下)、KeyEvent.KEYCODE_DPAD_LEFT(向左)、KeyEvent.KEYCODE_DPAD_RIGHT(向右)、KeyEvent.KEYCODE_DPAD_UP(向上)作比较。然后作居中、下移、左移、右移、上移的操作。
下面我们来看看程序,首先是布局页面main.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"
>
<Button
android:id="@+id/Button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/click">
</Button>
</AbsoluteLayout>
注意:布局页面必须是绝对定位布局-AbsoluteLayout,否则程序会报错。
主程序OnKeyDownTest.java

package test.shi;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.Toast;


public class OnKeyDownTest extends Activity implements OnClickListener {
private Button mButton;

// 屏幕的宽高
private int intScreenX, intScreenY;
//mButton的宽
private int intWidth = 100;
//mButton的高
private int intHeight = 50;
//mButton右上角的坐标位置
private int intButtonX, intButtonY;
// 平移量
int intShift = 10;
DisplayMetrics dm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

this.Init();
mButton.setOnClickListener(this);
}

public void Init() {
mButton = (Button) this.findViewById(R.id.Button01);
dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
// 得到屏幕的宽
intScreenX = dm.widthPixels;
// 得到屏幕的高
intScreenY = dm.heightPixels;

// Button居中
RestoreButton();
}

@SuppressWarnings("deprecation")
private void RestoreButton() {
//水平居中定位
intButtonX = (intScreenX - intWidth) / 2;
//垂直居中定位
intButtonY = (intScreenY - intHeight) / 2;
//设置按钮位置
mButton.setLayoutParams(new AbsoluteLayout.LayoutParams(intWidth,
intHeight, intButtonX, intButtonY));

Toast.makeText(
this,
"(" + Integer.toString(intButtonX) + ","
+ Integer.toString(intButtonY) + ")",
Toast.LENGTH_SHORT).show();
}

/** 点击按钮时,按钮重新居中定位. */
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Button01:
RestoreButton();
}
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
RestoreButton();
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
Down();
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
Left();
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
Right();
break;
case KeyEvent.KEYCODE_DPAD_UP:
Up();
break;
case KeyEvent.KEYCODE_VOLUME_DOWN:
//声音控制按键,此处是需要自己写的处理代码
break;
case KeyEvent.KEYCODE_VOLUME_UP:
//声音控制按键,此处是需要自己写的处理代码
break;
case KeyEvent.KEYCODE_UNKNOWN:
//不能判断未来的手机的按键,此处是需要自己写的处理代码
break;

/**还有字幕按键KEYCODE_A........KEYCODE_Z的按键处理*/
}
return super.onKeyDown(keyCode, event);
}

private void Up() {
// TODO Auto-generated method stub
intButtonY = intButtonY - intShift;
// 超过边界时处理,就放置在边上
if (intButtonY < 0) {
intButtonY = 0;
}
refresh();
}

private void Right() {
// TODO Auto-generated method stub
intButtonX = intButtonX + intShift;
if (intButtonX > intScreenX - intWidth) {
intButtonX = intScreenX - intWidth;
}
refresh();
}

private void Left() {
// TODO Auto-generated method stub
intButtonX = intButtonX - intShift;
if (intButtonX < 0) {
intButtonX = 0;
}
refresh();
}

private void Down() {
// TODO Auto-generated method stub
intButtonY = intButtonY + intShift;
if (intButtonY > intScreenY - intHeight) {
intButtonY = intScreenY - intHeight;
}
refresh();
}

@SuppressWarnings("deprecation")
private void refresh() {
mButton.setLayoutParams(new AbsoluteLayout.LayoutParams(intWidth,
intHeight, intButtonX, intButtonY));
Toast.makeText(this, "(" + intButtonX + "," + intButtonY + ")",
Toast.LENGTH_SHORT).show();
}
}

运行效果就是点击键盘上的上移、左移、下移、右移、居中按钮,页面上的按钮做相应的移动,位置发生变化。
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载