Android Keyboard Events and screen events operating principles and interaction implementation

Source: Internet
Author: User
Tags gety

When customizing views or playing games, we often use keyboard-triggered events and screen-triggered events! Keyboard-triggered events (such as onKeyDown (int keyCode, KeyEvent event) and screen-triggered events (onTouchEvent) in a custom View )) how do I interact with keyboard-triggered events (such as onKeyDown (int keyCode, KeyEvent event) and screen-triggered events (onTouchEvent) in an activity? What is the operating principle? Let's take a look:

1. Screen trigger event: the activity or view in which the control triggers the screen trigger event in the upper layer. If the return value is default or false, the screen trigger event will be passed down one by one! If the returned value is true, it will not be passed down after the action is executed!
2. keyboard-triggered events: This principle is similar to that of screen-triggered events. However, when the control is not set to focus, generally, the buttons on the keyboard trigger events in the activity (for example, onKeyDown (int keyCode, KeyEvent event )).

The following is an example.:
For example, when playing games, we often customize views. We generally like and use the keyboard trigger events and screen trigger events in this view (GameView), but for example: onKeyDown (int keyCode, keyEvent). Generally, when we press the keyboard, the keyboard trigger event in the activity is directly triggered. How can we make it directly trigger the keyboard trigger event (GameView?

Method 1:
Example 1:Copy codeThe Code is as follows: View Code
Package net. loonggg. project;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. view. MotionEvent;
Public class MainActivity extends Activity {
Private GameView gameView;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
GameView = new GameView (this );
SetContentView (gameView );
}
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
System. out. println ("MainActivity:" + "keyCode:" + keyCode + "evnet :"
+ Event );
Return super. onKeyDown (keyCode, event );
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
System. out. println ("MainActivity:" + "event:" + event + "x :"
+ Event. getX () + "y:" + event. getY ());
Return super. onTouchEvent (event );
}
}
Package net. loonggg. project;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. view. KeyEvent;
Import android. view. MotionEvent;
Import android. view. View;
Public class GameView extends View {
Private Paint paint;
Private int pointX;
Private int pointY;
Public GameView (Context context ){
Super (context );
// Set GameView to get focus
This. setFocusable (true );
Paint = new Paint ();
Paint. setColor (Color. YELLOW );
Thread t = new Thread (new MyThread ());
T. start ();
}
@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
Canvas. drawCircle (pointX, pointY, 15, paint );
}
Class MyThread implements Runnable {
@ Override
Public void run (){
While (true ){
Try {
Thread. sleep (3000 );
PointY = pointY + 10;
PointX = pointX + 10;
If (startx> 200 ){
Break;
}
} Catch (InterruptedException e ){
E. printStackTrace ();
}
GameView. this. postInvalidate ();
}
}
}
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
System. out. println ("GameView:" + "keyCode:" + keyCode + "evnet :"
+ Event );
// This event will not be propagated
Return true;
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
System. out. println ("GameView:" + "event:" + event + "x :"
+ Event. getX () + "y:" + event. getY ());
// This event will not be propagated
Return true;
}
}

Method 2:Copy codeThe Code is as follows: View Code
Package net. loonggg. project;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. view. MotionEvent;
Public class MainActivity extends Activity {
Private GameView gameView;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
GameView = new GameView (this );
SetContentView (gameView );
}
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
System. out. println ("MainActivity:" + "keyCode:" + keyCode + "evnet :"
+ Event );
// Pass the event to gameView
GameView. onKeyDown (keyCode, event );
Return super. onKeyDown (keyCode, event );
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
System. out. println ("MainActivity:" + "event:" + event + "x :"
+ Event. getX () + "y:" + event. getY ());
// Pass the event to gameView
GameView. onTouchEvent (event );
Return super. onTouchEvent (event );
}
}
Package net. loonggg. project;
Import android. content. Context;
Import android. graphics. Canvas;
Import android. graphics. Color;
Import android. graphics. Paint;
Import android. view. KeyEvent;
Import android. view. MotionEvent;
Import android. view. View;
Public class GameView extends View {
Private Paint paint;
Private int pointX;
Private int pointY;
Public GameView (Context context ){
Super (context );
Paint = new Paint ();
Paint. setColor (Color. YELLOW );
Thread t = new Thread (new MyThread ());
T. start ();
}
@ Override
Protected void onDraw (Canvas canvas ){
Super. onDraw (canvas );
Canvas. drawCircle (pointX, pointY, 15, paint );
}
Class MyThread implements Runnable {
@ Override
Public void run (){
While (true ){
Try {
Thread. sleep (3000 );
PointY = pointY + 10;
PointX = pointX + 10;
If (startx> 200 ){
Break;
}
} Catch (InterruptedException e ){
E. printStackTrace ();
}
GameView. this. postInvalidate ();
}
}
}
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
System. out. println ("GameView:" + "keyCode:" + keyCode + "evnet :"
+ Event );
Return super. onKeyDown (keyCode, event );
}
@ Override
Public boolean onTouchEvent (MotionEvent event ){
System. out. println ("GameView:" + "event:" + event + "x :"
+ Event. getX () + "y:" + event. getY ());
Return super. onTouchEvent (event );
}
}

Here, the second method is recommended because some simulators in the first method cannot be used!
You do not understand the world of the arms, you do not know the life of the arms, and you only know the world of programmers. Programmers, strive for their own wonderful world, and work hard! Come on ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.