DPAD按键处理

/*
 * DPAD按键处理
 * 当程序onCreate()时就调用RestoreButton(),这是一个自定义
 * 的方法,目的是判断手机的屏幕分辨率,并将按钮居中存放。
 * Activity重写了onKeyDown()方法,其传入的参数是keyCode和
 * keyEvent,前者为按键代码,后者为KeyEvent对象,其常见的构建
 * 方法是KeyEvent(int action,int code)。
 * keyCode是与KeyEvent.KEYCODE_*进行比较来做判断的。
 */
import 略;
public class Ex05_20Activity extends Activity {
	private Button bt;
	private int intWidth, intHeight, intButtonX, intButtonY;
	// 保存屏幕的分辨率
	private int intScreenX, intScreenY;
	// 按钮位移的平移量
	private int intShift = 2;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		// 获取屏幕的分辨率
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		intScreenX = dm.widthPixels;
		intScreenY = dm.heightPixels;
		// 定义按钮的宽高
		intWidth = 80;
		intHeight = 60;
		bt = (Button) findViewById(R.id.myButton1);
		// 初始化按钮的位置
		RestoreButton();
		// 当按下按钮,还原初始化位置
		bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				RestoreButton();
			}
		});
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_DPAD_CENTER:
			RestoreButton();
			break;
		case KeyEvent.KEYCODE_DPAD_UP:
			MoveButtonUp();
			break;
		case KeyEvent.KEYCODE_DPAD_DOWN:
			MoveButtonDown();
			break;
		case KeyEvent.KEYCODE_DPAD_LEFT:
			MoveButtonLeft();
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			MoveButtonRight();
			break;
		default:
			break;
		}
		return super.onKeyDown(keyCode, event);
	}

	@SuppressWarnings("deprecation")
	private void MoveButtonRight() {
		// TODO Auto-generated method stub
		intButtonX = intButtonY - intShift;
		if (intButtonX > (intScreenX - intHeight)) {
			intButtonX = intScreenX - intHeight;
			bt.setLayoutParams(new AbsoluteLayout.LayoutParams(intWidth,
					intHeight, intButtonX, intButtonY));
		}
	}

	@SuppressWarnings("deprecation")
	private void MoveButtonLeft() {
		// TODO Auto-generated method stub
		intButtonX = intButtonX- intShift;
		if (intButtonY < 0) {
			intButtonY = 0;
			bt.setLayoutParams(new AbsoluteLayout.LayoutParams(intWidth,
					intHeight, intButtonX, intButtonY));
		}
	}

	@SuppressWarnings("deprecation")
	private void MoveButtonDown() {
		// TODO Auto-generated method stub
		intButtonY = intButtonY + intShift;
		if (intButtonY > (intScreenY - intHeight)) {
			intButtonY = intScreenY - intHeight;
			bt.setLayoutParams(new AbsoluteLayout.LayoutParams(intWidth,
					intHeight, intButtonX, intButtonY));
		}
	}

	@SuppressWarnings("deprecation")
	private void MoveButtonUp() {
		// TODO Auto-generated method stub
		intButtonY = intButtonY - intShift;
		if (intButtonY < 0) {
			intButtonY = 0;
			bt.setLayoutParams(new AbsoluteLayout.LayoutParams(intWidth,
					intHeight, intButtonX, intButtonY));
		}
	}

	@SuppressWarnings("deprecation")
	private void RestoreButton() {
		// TODO Auto-generated method stub
		intButtonX = (intScreenX - intWidth) / 2;
		intButtonY = (intScreenY - intHeight) / 2;
		Toast.makeText(Ex05_20Activity.this, "(" + Integer.toString(intButtonX)
				+ "," + Integer.toString(intButtonY), Toast.LENGTH_SHORT);
		bt.setLayoutParams(new AbsoluteLayout.LayoutParams(intWidth, intHeight,
				intButtonX, intButtonY));
	}

}

main.xml文件代码:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/white"
    android:orientation="vertical" >

    <Button
        android:id="@+id/myButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="20px"
        android:layout_y="40px"
        android:text="@string/str_button1" />

</AbsoluteLayout>



   

你可能感兴趣的:(DPAD按键处理)