android 鍵盤事件和螢幕事件的運行原理及互動實現

來源:互聯網
上載者:User

當在自訂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);
}
}

在這裡,推薦使用第二種方法,因為第一種方法有的模擬器不能夠使用!
囧神的世界你不懂,蟲哥的生活你沒有,只有程式猿的世界大家才知道。程式猿們,為了自己的精彩世界奮鬥吧,努力吧!加油……

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.