The operating principle and interactive implementation of Android keyboard events and screen events _android

Source: Internet
Author: User
Tags gety
When customizing view or playing games, we often use the keyboard to trigger events and screen triggers! Keyboard triggers events in a custom view (e.g. OnKeyDown (int keycode, keyevent event)) and screen triggering events (Ontouchevent (Motionevent event) And how are keyboard triggers (such as: OnKeyDown (int keycode, keyevent event)) and screen triggering events (Ontouchevent (Motionevent event)) interacting with the activity? What is a principle of operation? Let's take a look at the following:

1, screen trigger event: Which activity or view, the control at the top of the first trigger in this control screen triggering event, return value if it is the default or false will be a layer of down to pass! If the return value is true, it will not be passed down after the action is done!
2, the keyboard trigger event: This principle and screen trigger event is similar, but different, when not set control in focus, generally press the keys on the keyboard trigger is the activity in the keyboard trigger events (such as: OnKeyDown int keycode, keyevent Event)).

Here's an example.
For example, when playing games, we often customize the view, we generally like and use the trigger in this view (Gameview) Trigger events and screen triggering events, but for example: onKeyDown (int keycode, keyevent event)), In general, when we press the keyboard directly triggered by the activity in the keyboard trigger event, how to let it directly trigger (Gameview) in the keyboard trigger events?

The first of these methods
Example 1:
Copy Code code 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 {
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, paint);
}
Class Mythread implements Runnable {
@Override
public void Run () {
while (true) {
try {
Thread.Sleep (3000);
Pointy = pointy + 10;
Pointx = Pointx + 10;
if (Pointx > 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 spread out
return true;
}
@Override
public boolean ontouchevent (Motionevent event) {
System.out.println ("Gameview:" + "event:" + Event + "x:"
+ event.getx () + "y:" + event.gety ());
This event will not spread out
return true;
}
}

The second method
Copy Code code 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 {
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, paint);
}
Class Mythread implements Runnable {
@Override
public void Run () {
while (true) {
try {
Thread.Sleep (3000);
Pointy = pointy + 10;
Pointx = Pointx + 10;
if (Pointx > 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);
}
}

In this case, the second method is recommended, because the first method has a simulator that cannot be used!
You do not understand the world of God, the life of the worm you do not, only the process of the world of apes people know. Program apes, for their own wonderful World struggle bar, efforts! 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.