android 键盘事件和屏幕事件的运行原理及交互实现
程序员文章站
2023-12-02 19:53:16
当在自定义view或者做游戏的时候,我们常常会用到键盘触发事件和屏幕触发事件!在自定义的view里的键盘触发事件(比如:onkeydown(int keycode, key...
当在自定义view或者做游戏的时候,我们常常会用到键盘触发事件和屏幕触发事件!在自定义的view里的键盘触发事件(比如:onkeydown(int keycode, keyevent event))和屏幕触发事件(ontouchevent(motionevent event))和activity里的键盘触发事件(比如:onkeydown(int keycode, keyevent event))和屏幕触发事件(ontouchevent(motionevent event))是怎么样交互的呢?是怎样的一个运行原理呢?下面来看看:
1、屏幕触发事件:哪个activity或者视图,控件在最上层就最先触发这个控件里的屏幕触发事件,返回值如果是默认或者false就会一层一层的往下传递!如果返回值是true,则在执行完动作之后不会往下传递!
2、键盘触发事件:这个原理和屏幕触发事件差不多,但是不同的时,在没有设置控件处于焦点时,一般按键盘里的按键触发的是activity里的键盘触发事件(比如:onkeydown(int keycode, keyevent event))。
下面来举个例子:
比如在做游戏的时候,我们经常自定义视图,我们一般都喜欢和使用触发本视图(gameview)里的键盘触发事件和屏幕触发事件,但是比如:onkeydown(int keycode, keyevent event)),一般我们按键盘的时候直接触发的是activity里的键盘触发事件,怎样让它直接触发(gameview)里的键盘触发事件呢?
第一种方法:
例子1:
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);
// 设置gameview获得焦点
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 (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 true;
}
@override
public boolean ontouchevent(motionevent event) {
system.out.println("gameview:" + " event:" + event + " x:"
+ event.getx() + " y:" + event.gety());
//此事件不会传播出去
return true;
}
}
第二种方法:
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);
//把事件传递给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());
//把事件传递给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 (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);
}
}
在这里,推荐使用第二种方法,因为第一种方法有的模拟器不能够使用!
囧神的世界你不懂,虫哥的生活你没有,只有程序猿的世界大家才知道。程序猿们,为了自己的精彩世界奋斗吧,努力吧!加油……
1、屏幕触发事件:哪个activity或者视图,控件在最上层就最先触发这个控件里的屏幕触发事件,返回值如果是默认或者false就会一层一层的往下传递!如果返回值是true,则在执行完动作之后不会往下传递!
2、键盘触发事件:这个原理和屏幕触发事件差不多,但是不同的时,在没有设置控件处于焦点时,一般按键盘里的按键触发的是activity里的键盘触发事件(比如:onkeydown(int keycode, keyevent event))。
下面来举个例子:
比如在做游戏的时候,我们经常自定义视图,我们一般都喜欢和使用触发本视图(gameview)里的键盘触发事件和屏幕触发事件,但是比如:onkeydown(int keycode, keyevent event)),一般我们按键盘的时候直接触发的是activity里的键盘触发事件,怎样让它直接触发(gameview)里的键盘触发事件呢?
第一种方法:
例子1:
复制代码 代码如下:
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);
// 设置gameview获得焦点
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 (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 true;
}
@override
public boolean ontouchevent(motionevent event) {
system.out.println("gameview:" + " event:" + event + " x:"
+ event.getx() + " y:" + event.gety());
//此事件不会传播出去
return true;
}
}
第二种方法:
复制代码 代码如下:
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);
//把事件传递给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());
//把事件传递给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 (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);
}
}
在这里,推荐使用第二种方法,因为第一种方法有的模拟器不能够使用!
囧神的世界你不懂,虫哥的生活你没有,只有程序猿的世界大家才知道。程序猿们,为了自己的精彩世界奋斗吧,努力吧!加油……