扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
KEY_NUM0
KEY_NUM1
KEY_NUM2
KEY_NUM3
KEY_NUM4
KEY_NUM5
KEY_NUM6
KEY_NUM7
KEY_NUM8
KEY_NUM9
KEY_STAR
KEY_POUND
这些都是按键基本值,当然有些手机是触摸屏的。事件的传递是通过如下方法来实现的。
protected void keyPressed(int keyCode)
protected void keyReleased(int keyCode)
protected void keyRepeated(int keyCode)
protected void pointerPressed(int x, int y)
protected void pointerDragged(int x, int y)
protected void pointerReleased(int x, int y)
protected void showNotify()
protected void hideNotify()
protected abstract void paint(Graphics g)
commandAction() method of the CommandListener interface
开发者应该继承Canvas类的同时覆盖相应的方法来实现低级事件的处理,低级事件的处理是连续的、串行的,在前面的事件返回之前后面的事件不会执行,当然也有例外,你可以参考API doc得到答案。下面是一个例子,它的功能就是在屏幕上显示出你按下键的键值,界面非常简单因此没有抓图。代码如下所示:
//LowLevelMIDlet.java
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/*
* Created on 2004-6-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class LowLevelMIDlet extends MIDlet
{
private Display display;
private KeyCanvas keyCanvas;
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException
{
// TODO Auto-generated method stub
display = Display.getDisplay(this);
keyCanvas = new KeyCanvas();
display.setCurrent(keyCanvas);
}
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp()
{
// TODO Auto-generated method stub
}
/*
* (non-Javadoc)
*
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
// TODO Auto-generated method stub
}
}
//KeyCanvas.java
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
/*
* Created on 2004-6-25
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class KeyCanvas extends Canvas implements CommandListener
{
private String key = "Press any key";
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
*/
protected void paint(Graphics g)
{
// TODO Auto-generated method stub
g.setColor(255, 255, 255);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0, 0, 0);
g.drawString(key, getWidth() / 2, getHeight() / 2, Graphics.TOP
| Graphics.HCENTER);
}
public void keyPressed(int keyCode)
{
key = keyCode + " is pressed";
repaint();
}
public void keyReleased(int keyCode)
{
key = keyCode + " is released";
repaint();
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
* javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command arg0, Displayable arg1)
{
// TODO Auto-generated method stub
}
}
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者