DPAD 키 처리

5004 단어
/*
* DPAD 키 처리
* Restore Button () 은 프로그램 onCreate () 에서 호출됩니다. 이것은 사용자 정의입니다.
*핸드폰의 화면 해상도를 판단하고 버튼을 가운데에 두는 것이 목적이다.
* Activity에서 onKeyDown () 메서드를 다시 썼으며 키 코드와
*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>
   

좋은 웹페이지 즐겨찾기