日期:2014-05-20  浏览次数:20696 次

J2ME手机键盘测试例子

今天有空,想想自己一个月前学习J2ME的时候,一直不知道如何编写监听手机键盘的代码,现在把我当时的想法写下来吧。

先贴一个入门级别的代码,再分析:

package com.srk.local.key;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;


public class KeyActionMIDlet extends MIDlet implements CommandListener {
??? private Command exitCommand;
??? private MainCanvas currentDisplay;
??? public KeyActionMIDlet() {
??????? currentDisplay = new MainCanvas();
??????? exitCommand = new Command("退出", Command.EXIT, 1);
??? }

??? protected void startApp() throws MIDletStateChangeException {
??????? System.out.println("startApp()!");
??????? currentDisplay.addCommand(exitCommand);
??????? new Thread(currentDisplay).start();
??????? currentDisplay.setCommandListener(this);
??????? Display.getDisplay(this).setCurrent(currentDisplay);
??? }

??? protected void pauseApp() {
??????? System.out.println("pauseApp()!");
??? }

??? protected void destroyApp(boolean _boolean) throws
??????????? MIDletStateChangeException {
??????? System.out.println("destroyApp()!");
??? }

??? public void commandAction(Command command, Displayable displayable) {
??????? if (command == exitCommand) {
??????????? try {
??????????????? destroyApp(false);
??????????????? notifyDestroyed();
??????????? } catch (MIDletStateChangeException e) {
??????????????? e.printStackTrace();
??????????? }
??????? }
??? }

}


class MainCanvas extends Canvas implements Runnable {
??? private String buttonPressed;
??? private boolean leftPressed, rightPressed, upPressed, downPressed;
??? private int px, py, pu, pd;
??? public MainCanvas() {
??????? init();
??? }

??? public void paint(Graphics g) {
??????? g.setColor(0xFFFFFF);
??????? g.fillRect(0, 0, this.getWidth(), this.getHeight());
??????? g.setColor(0x000000);
??????? g.drawString(buttonPressed, this.getWidth() / 2, this.getHeight() / 2,
???????????????????? Graphics.LEFT | Graphics.TOP);
??????? g.drawString("J2ME", px, py, Graphics.HCENTER | Graphics.TOP);
??? }

??? public void run() {
??????? while (true) {
??????????? if (leftPressed) {
??????????????? left();
??????????? }
??????????? if (rightPressed) {
??????????????? right();
??????????? }
??????????? if (upPressed) {
??????????????? up();
??????????? }
??????????? if (downPressed) {
??????????????? down();
??????????? }
??????????? try {
??????????????? Thread.sleep(50);
??????????? } catch (InterruptedException e) {
??????????????? e.printStackTrace();
??????????? }
??????? }

??? }

??? protected void keyPressed(int keyCode) {
??????? int action = getGameAction(keyCode);
??????? switch (action) {
??????? case LEFT:
??????????? left();
??????????? leftPressed = true;
??????????? break;
??????? case RIGHT:
??????????? right();
??????????? rightPressed = true;
??????????? break;
??????? case UP:
??????????? up();
??????????? upPressed = true;
??????????? break;
??????? case DOWN:
??????????? down();
??????????? downPressed = true;
??????????? break;
??????? default:
??????????? break;
??????? }
??????? repaint();
??? }

??? protected void keyReleased(int keyCode) {
??????? int action = getGameAction(keyCode);
??????? switch (action) {
??????? case LEFT:
??????????? leftPressed = false;
??????????? buttonPressed = "";
??????????? break;
??????? case RIGHT:
??????????? rightPressed = false;
??????????? buttonPressed = "";
??????????? break;
??????? case UP:
??????????????? upPressed = false;
??????????????? buttonPressed ="";
??????????????? break;
??????? case DOWN:
??????????? downPressed = false;
??????????? buttonPressed = "";
??????????? break;
??????? default:
??????????? break;
??????? }
??????? repaint();
??? }

??? protected void keyRepeated(int keyCode) {
??????? int action = getGameAction(keyCode);
??????? switch (action) {
??????? case LEFT:
??????????? left();
??????????? break;
??????? case RIGHT:
??????????? right();
??????????? break;
??????? case UP:
??????????? up();
??????? case DOWN:
??????????? down();
??????? default:
??????????? break;
??????? }
??????? repaint();
??? }

??? public void init() {
??????? buttonPressed = "";
??????? px = this.getWidth();
??????? py = this.getHeight();
??? }

??? public void left() {
??????? if (px >= 0) {
??????????? px--;
??????? }
??????? buttonPressed = "左";
??????? repaint();
??? }

??? public void right() {
??????? if (px <= this.getWidth()) {
??????????? px++;
??????? }
??????? buttonPressed = "右";
??????? repaint();
??? }

??? public void up() {
??????? if (pu <=this.getHeight()) {
??????????? pu++;
??????? }
??????? buttonPressed = "上";
??????? repaint();
??? }

??? public void down() {
??????? if (pd >=0 ) {
??????????? pd--;
??????? }
??????? buttonPressed = "下";
??????? repaint();
??? }

}

要看懂这段代码首先需要理解的知识:

1,j2me管理应用程序的三个抽象方法

2,j2me的线程知识(注意与J2SE的线程的区别)

3,Canvas类的应用

4,Java基础知识

有了上面的知识后写上面的代码简直是小菜一叠。首先查看一下API,我们可以知道在J2ME中的事件传输是串行化的,那么什么是串行化呢?java doc里面说当一个时间方法调用完成之后,下面的事件方法才会被调用。这也很好理解嘛,”串“的字面意思不就是这样的意思吗?所以在遇到编程方面的词义的时候要不要把问题复杂化了,毕竟老外也不比咱们聪明嘛。好啦,仔细看一下API我们会知道,Canvas类定义很多静态的变量和很多的方法,里面要注意静态变量和一个抽象方法protected abstract void paint(Graphics?g),,所以我们继承Canvas类的时候must be要实现这个抽象方法了,因为我们没有把我们自己的类再次声明成abstract。下面摘抄一小段API的片断

Event Delivery

The Canvas object defines several methods that are called by the implementation. These methods are primarily for the purpose of delivering events to the application, and so they are referred to as event delivery methods. The set of methods is:

  • showNotify()
  • hideNotify()
  • keyPressed()
  • keyRepeated()
  • keyReleased()
  • pointerPressed()
  • pointerDragged()
  • pointerReleased()
  • paint()

说的就是“串”的意思哦,呵呵,英语不好也就遇到big problem了,有了上面的知识后再略微注意一下另外的方法getGameAction(int keyCode)和getKeyName(int keyCode)就可以写出下面更简便的方法了。

package com.srk.local.key;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Display;

public class KeyCodesName extends MIDlet {
??? private KeyCodeCanvas currentDisplay;
??? public KeyCodesName() {
??????? currentDisplay = new KeyCodeCanvas(this);
??? }

??? protected void startApp() throws MIDletStateChangeException {
??????? Display.getDisplay(this).setCurrent(currentDisplay);
??? }

??? protected void pauseApp() {
??? }

??? protected void destroyApp(boolean _boolean) throws
??????????? MIDletStateChangeException {
??? }
??? public void exitApp() {
??????? try {
??????????? destroyApp(false);
??????????? notifyDestroyed();
??????? } catch(MIDletStateChangeException e) {
??????????? e.printStackTrace();
??????? }
??? }
}
class KeyCodeCanvas extends Canvas implements CommandListener {
??? private Command exitCommand;
??? private String keyText ;
??? private KeyCodesName keyMidlet;
??? public KeyCodeCanvas(KeyCodesName keyMilet) {
??????? this.keyMidlet = keyMidlet;
??????? keyText = "哈哈,开始吧 !";
??????? exitCommand = new Command("退出",Command.EXIT,1);
??????? this.addCommand(exitCommand);
??????? this.setCommandListener(this);
??? }
??? public void paint(Graphics g) {
??????? System.out.println("本程序正在被调用了,呵呵!");
??????? g.setColor(0,255,0);
??????? g.fillRect(0,0,this.getWidth(),this.getHeight());
??????? if(keyText!=null) {
??????????? g.setColor(0,0,0);
??????????? g.drawString(keyText,this.getWidth() /2,this.getHeight() /2,Graphics.HCENTER|Graphics.TOP);
??????? }
??? }
??? public void keyPressed(int keyCode) {
??????? keyText = this.getKeyName(keyCode);
??????? repaint();
??????? try {
??????????? Thread.sleep(50);
??????? } catch(InterruptedException e) {
??????????? System.out.println("keyPressed Exception!");
??????????? e.printStackTrace();
??????? }
??? }
??? public void showNotify() {
??????? repaint();
??????? try {
??????????? Thread.sleep(50);
??????? } catch(InterruptedException e) {
??????????? e.printStackTrace();
??????? }
??? }
??? public void commandAction(Command c,Displayable d) {
??????? if(c == exitCommand){
??????????? keyMidlet.exitApp();
??????? }
??? }
}

1 楼 datou 2007-10-07