如何实现给android系统发送一些键盘事件和屏幕事件
2016-09-16 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
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;
}
}
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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询