The purpose of our code is very simple, that is, to listen to the trigger of these keys in the top, bottom, and left. Directly run the Code:
DPAD. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <absolutelayout <br/> xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> Android: background = "# ffffff" <br/> <button <br/> Android: Id = "@ + ID/mybutton1" <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: text = "Move me" <br/> Android: layout_x = "20px" <br/> Android: layout_y = "40px"/> <br/> </absolutelayout> <br/>
Activity Code:
Package cn.com. chenzheng_java; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. util. displaymetrics; <br/> Import android. view. keyevent; <br/> Import android. widget. absolutelayout; <br/> Import android. widget. button; <br/> Import android. widget. toast; <br/>/** <br/> * @ description controls the up, down, left, and right direction keys of the mobile phone. <br/> * @ author chenzheng_java <br/> */< br/> public class dpada Ctivity extends activity {<br/> button; <br/> displaymetrics metrics = new displaymetrics (); <br/> int screenx = 0; // screen width <br/> int screeny = 0; // screen height <br/> int buttonwidth = 80; // button width <br/> int buttonheight = 40; // button height <br/> int currentx = 0; // The current X coordinate of the button <br/> int currenty = 0; // The current y coordinate of the button <br/> int step = 0; // step size when moving <br/> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> Super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. DPAD); <br/> button = (button) findviewbyid (R. id. mybutton1); <br/> getwindowmanager (). getdefadisplay display (). getmetrics (metrics); <br/> screenx = metrics. widthpixels; <br/> screeny = metrics. heightpixels; <br/>/* buttonwidth = button. getwidth (); <br/> buttonheight = button. getheight (); */</P> <p> currentx = (screenx-buttonwidth)/2; <br/> currenty = (Screeny-buttonheight)/2; <br/> step = 2; </P> <p> button. setlayoutparams (New absolutelayout. layoutparams (buttonwidth, buttonheight, currentx, currenty )); </P> <p >}</P> <p>/** <br/> * when the current left and right keys are pressed, triggered (this is a prerequisite, that is, the current activity must not have a view listening button <br/> *. For example, if an edittext is waiting for input, when DPAD is pressed, the event is not triggered.) <br/> * activity. onkeydown (); <br/> A key is triggered when it is pressed, but it is not processed by any view in the activity. <Br/> by default, when the keycode_back key is pressed, the previous activity is returned. <Br/> */<br/> @ override <br/> Public Boolean onkeydown (INT keycode, keyevent event) {<br/> switch (keycode) {<br/> case keyevent. keycode_dpad_down: // press the down key <br/> movedown (); <br/> break; <br/> case keyevent. keycode_dpad_up: // press the up key <br/> moveup (); <br/> case keyevent. keycode_dpad_left: // left click <br/> moveleft (); <br/> case keyevent. keycode_dpad_right: // right-click <br/> moveright (); <br/> default: <br/> break; <br/>}</P> <P> return Super. onkeydown (keycode, event); <br/>}</P> <p> @ suppresswarnings ("deprecation") <br/> private void movedown () {<br/> int temp = currenty + step; <br/> If (temp> (screeny-buttonheight) {<br/> showtoast ("it's time to get started! "); <Br/> button. setlayoutparams (New absolutelayout. layoutparams (buttonwidth, buttonheight, screenx, screeny-buttonheight); <br/>}< br/> else {<br/> currenty = currenty + step; <br/> absolutelayout. layoutparams Params = <br/> New absolutelayout. layoutparams (buttonwidth, buttonheight, currentx, currenty); <br/> button. setlayoutparams (Params); </P> <p >}< br/> // button. setlayoutparams (New absolutelayout. layoutp Arams (buttonwidth, buttonheight, currentx, currentY-2); </P> <p >}</P> <p> @ suppresswarnings ("deprecation ") <br/> private void moveup () {<br/> int temp = currenty-step; <br/> If (temp <= 0) {<br/> showtoast ("It's up! "); <Br/> button. setlayoutparams (New absolutelayout. layoutparams (buttonwidth, buttonheight, screenx, 0); <br/>}< br/> else {<br/> currenty = currenty-step; <br/> absolutelayout. layoutparams Params = <br/> New absolutelayout. layoutparams (buttonwidth, buttonheight, currentx, currenty); <br/> button. setlayoutparams (Params); <br/>}</P> <p> @ suppresswarnings ("deprecation ") <br/> private void moveleft (){ <Br/> int temp = currentx-step; <br/> If (temp <= 0) {<br/> showtoast ("head to the left! "); <Br/> button. setlayoutparams (New absolutelayout. layoutparams (buttonwidth, buttonheight, 0, screeny); <br/>}< br/> else {<br/> currentx = currentx-step; <br/> absolutelayout. layoutparams Params = <br/> New absolutelayout. layoutparams (buttonwidth, buttonheight, currentx, currenty); <br/> button. setlayoutparams (Params); <br/>}</P> <p> @ suppresswarnings ("deprecation ") <br/> private void moveright () {<Br/> int temp = currentx + step; <br/> If (temp> = (screenx-buttonwidth )) {<br/> showtoast ("head to right! "); <Br/> button. setlayoutparams (New absolutelayout. layoutparams (buttonwidth, buttonheight, screenx-buttonwidth, currenty); <br/>}< br/> else {<br/> currentx = currentx + step; <br/> absolutelayout. layoutparams Params = <br/> New absolutelayout. layoutparams (buttonwidth, buttonheight, currentx, currenty); <br/> button. setlayoutparams (Params ); <br/>}</P> <p>/** <br/> * prompt message <br/> * @ Param text prompt information <br/> */<br/> private void showtoast (string text) {<br/> toast. maketext (this, text, toast. length_long ). show (); </P> <p >}< br/>
Bytes ---------------------------------------------------------------------------------------
Here we can see that to listen to these direction keys, we must override the public Boolean onkeydown (INT keycode, keyevent event) method, which is defined in activity, the keycode represents the identifier of the key you clicked, And the keyevent is an event.
Note that the coordinates of the buttons we control are actually the coordinates in the upper left corner. While the screen is directed to the left X axis and down to the Y axis. That is to say, the coordinates of the components displayed in the screen are all positive.
Of course, all the keys in the keyboard can be listened to, we can also listen to the A-Z of these keys, their keycode in turn is keycode_a ------- keycode_z.
Here is a tips, you can determine what the user input A-Z that character, the Code is as follows:
Int code = 'A' + keycode-29; <br/> char CH = (char) Code; <br/>
If we cannot obtain the current button, we can use keycode_unknown to capture it.
Volume Control: keycode_volume_down/keycode_volume_up.
Bytes -----------------------------------------------------------------------------------------
For keyevent, it represents the event when we click the keyboard. you can create your own keyboard event.
The action in the parameter represents the user's operation. The value is one of action_down, action_up, and action_multiple. The Code represents our identifier. The keycode_volume_down mentioned above is a code.