服务器之家

服务器之家 > 正文

android 键盘事件和屏幕事件的运行原理及交互实现

时间:2020-12-30 13:17     来源/作者:Android开发网

当在自定义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);
}
}


在这里,推荐使用第二种方法,因为第一种方法有的模拟器不能够使用!
囧神的世界你不懂,虫哥的生活你没有,只有程序猿的世界大家才知道。程序猿们,为了自己的精彩世界奋斗吧,努力吧!加油……

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址
电视剧《琉璃》全集在线观看 琉璃美人煞1-59集免费观看地址 2020-08-12
返回顶部