當在自定義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);
}
}
在這裡,推薦使用第二種方法,因為第一種方法有的模擬器不能夠使用!
囧神的世界你不懂,蟲哥的生活你沒有,只有程序猿的世界大家才知道。程序猿們,為了自己的精彩世界奮斗吧,努力吧!加油……