- 超级莱鸟
- 等级:


- 性别:

- 文章: 19
- 积分: 120

|
看了几个小时的M$ COM组件,看得累了。打开Eclipse看看J2ME,想了如何实现这个输入界面的问题了。:)
继承自 Canvas的类,如果想直接调用输入法是不可能。如果想获得英文获得数字,可以通过捕获键盘事件来获得,如果你有字典库通过几个键的组合,也可以获得一 个输入法,但是全世界那么多种语言,每个语言可能有不同的输入法,拿中文来说,就有拼音,五笔等输入法。你不可能在你的应用中把输入法这块花费那么大的时 间,当然除非你是做输入法的。在某些特殊情况下,比如我想在手机上产生俄罗斯文字,而且不需要系统额外安装俄罗斯输入法(暂且这么叫吧),我可以根据俄文 字典,根据用户当前按键来产生俄文字符。这样是可以的。
我们今天说的是另一个思路。
我们用界面的外观来模仿他,通过键盘时间来改变当前的控件。比如系统提供的TextBox是可以改变输入法的。
首先,在我们的界面上画出你的布局。输入框边界用灰色画出,有焦点时候用蓝色画出。
比如我的界面可能有两个输入框一个是手机号码字段,一个是内容字段。
我定义如下参数
TextBox[] m_textBoxs = new TextBox[2];
//两个控件对应的字符串,打开TextBox时把对应的字符串付给控件,完成输入的时候把控件的文字放到对应的字符串里面。
String[] m_strValues = new String[2];
//移动UP,DOWN 来控制当前选中的控件。
int p_currentIndex=-1;
为每个TextBox放置两个Command,"完成","后退"
图一:启动发送短信界面

(图二:这个界面都是画出来的,尽量模仿吧)

(图三:移动UP,DONW键效果,当前选中内容)

(图四 选中内容框,按下ok键,显示对应的TextBox,可以在里面输入)

(图五:按下完成键,真实手机还有一个界面,模拟器上没有,此时把控件里面的字符串放置到数组中,兵重新画图,如果文字多行,要考虑换行,如果高度超过矩形的高度,要出现滚动条)

(图六:发送界面,把手机号码涂改了一下,点发送菜单就可以发短信了。)

完毕,继续看M$ COM了.
代码:
//这个代码是来自FormCanvas的扩展的。
java 代码
- import javax.microedition.io.Connector;
- import javax.microedition.lcdui.Alert;
- import javax.microedition.lcdui.AlertType;
- import javax.microedition.lcdui.Command;
- import javax.microedition.lcdui.CommandListener;
- import javax.microedition.lcdui.Displayable;
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.TextBox;
- import javax.microedition.lcdui.TextField;
- import javax.wireless.messaging.MessageConnection;
- import javax.wireless.messaging.TextMessage;
-
- import com.zhanglihai.j2me.event.IKeyListener;
- import com.zhanglihai.j2me.event.IMenuItemListener;
- import com.zhanglihai.j2me.ui.Component;
- import com.zhanglihai.j2me.ui.Constants;
- import com.zhanglihai.j2me.ui.FormCanvas;
- import com.zhanglihai.j2me.ui.Header;
- import com.zhanglihai.j2me.ui.KEvent;
- import com.zhanglihai.j2me.ui.Menu;
- import com.zhanglihai.j2me.ui.MenuBar;
- import com.zhanglihai.j2me.ui.MenuItem;
- import com.zhanglihai.j2me.ui.MenuItemEvent;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class SmsSendForm extends FormCanvas implements CommandListener,
- IKeyListener, IMenuItemListener {
-
- private TestJ2MEFramework parent;
-
- private TextBox mobileBox;
-
- private Command cmdOk = new Command("完成", Command.OK, 1);
-
- private Command cmdBack = new Command("后退", Command.BACK, 1);
-
- private MenuBar menuBar = null;
-
- private Header header = null;
-
- private Component label = null;
-
- private Menu mainMenu;
-
- private MenuItem itemSend;
-
- private MenuItem itemContent;
-
- private int currentField = -1;
-
- private TextBox contentBox = null;
-
- private String[] m_strValues = new String[2];;
-
- private TextBox[] m_textBoxs = new TextBox[2];
-
- public SmsSendForm(final TestJ2MEFramework parent) {
- this.parent = parent;
- this.mobileBox = new TextBox("手机号码", "", 11, TextField.PHONENUMBER);
- this.mobileBox.addCommand(cmdOk);
- this.mobileBox.addCommand(cmdBack);
- mobileBox.setCommandListener(this);
- this.contentBox = new TextBox("内容", "", 70, TextField.ANY);
- contentBox.addCommand(cmdOk);
- contentBox.addCommand(cmdBack);
- contentBox.setCommandListener(this);
- m_textBoxs[0] = mobileBox;
- m_textBoxs[1] = contentBox;
-
- this.menuBar = new MenuBar();
- this.header = new Header("发送短信");
- this.label = new Component("后退");
- label.setKeyListener(new IKeyListener() {
- public void actionPerformed(Object obj, KEvent e) {
- parent.setHome();
- }
- });
- this.mainMenu = new Menu("菜单");
- this.itemSend = new MenuItem("发送");
- this.itemContent = new MenuItem("内容");
- mainMenu.addItem(itemSend);
- mainMenu.addSeparator();
- mainMenu.addItem(itemContent);
- mainMenu.setItemListener(this);
- this.menuBar.setHeight(30);
- this.menuBar.setLabel(label);
- this.menuBar.setMenu(mainMenu);
- setHeader(header);
- setMenuBar(menuBar);
- setKeyPressedListener(this);
- setBackground(0xFFFFFF);
-
- }
-
- protected void drawWorkspace(Graphics g) {
- Font font = Constants.PLAIN_MEDIUM_FONT;
- g.setFont(font);
- g.setColor(0);
- String str = "手机号码";
- int offx = 5;
- int x = offx;
- int fh = font.getHeight();
- g.drawString(str, 5, 5 + (30 - fh) / 2, Graphics.LEFT | Graphics.TOP);
- if (currentField == 0)
- g.setColor(0x336699);
- else
- g.setColor(0xCCCCCC);
- x = offx + font.stringWidth(str) + 2;
- g.fillRect(x, 5, workspaceWidth - x - offx, 30);
- g.setColor(0xFFFFFF);
- g.fillRect(x + 2, 5 + 2, workspaceWidth - x - offx - 4, 30 - 4);
- char[] ch_str;
- int p_str_w = 0;
- int p_str_index = 0;
- if (m_strValues[0] != null) {
- g.setColor(0);
- ch_str = m_strValues[0].toCharArray();
- for (; p_str_index < ch_str.length; p_str_index++) {
- p_str_w += font.charWidth(ch_str[p_str_index]);
- if (p_str_w >= (workspaceWidth - x - offx - 4))
- break;
- }
- g.drawChars(ch_str, 0, p_str_index, x + 2, 5 + 2, Graphics.LEFT
- | Graphics.TOP);
- }
- str = "短信内容";
- g.setColor(0);
- g.drawString(str, offx, 40, Graphics.LEFT | Graphics.TOP);
- if (currentField == 1)
- g.setColor(0x336699);
- else
- g.setColor(0xCCCCCC);
- g.fillRect(offx, 40 + fh + 5, workspaceWidth - 10, workspaceHeight - 45
- - 2 - fh);
- g.setColor(0xFFFFFF);
- g.fillRect(offx + 2, 40 + fh + 5 + 2, workspaceWidth - 10 - 4,
- workspaceHeight - 45 - 2 - 4 - fh);
- int c_w = font.charWidth('中');
- if (m_strValues[1] != null) {
- g.setColor(0);
-
- ch_str = m_strValues[1].toCharArray();
- p_str_w = p_str_index = 0;
- int p_line = 0;
- x = 0;
- int y = 0;
- for (; p_str_index < ch_str.length; p_str_index++) {
- if (p_str_w >= (workspaceWidth - 10 - 4 - c_w)) {
- p_line++;
- p_str_w = 0;
- }
-
- x = offx + 2 + p_str_w;
- y = 40 + fh + 5 + 2 + p_line * fh;
-
- if (y >= workspaceHeight - 45 - 2 - 4 - fh)
- break;
- g.drawChar(ch_str[p_str_index], x, y, Graphics.LEFT
- | Graphics.TOP);
- p_str_w += font.charWidth(ch_str[p_str_index]);
- }
- }
- }
-
- public void actionPerformed(Object obj, KEvent e) {
- switch (e.gameAction) {
- case UP:
- currentField--;
- if (currentField < 0)
- currentField = 0;
- repaint();
- break;
- case DOWN:
- currentField++;
- if (currentField > 1)
- currentField = 1;
- repaint();
- break;
- default:
- if (e.keyCode == CENTER_OK_KEY) {
- parent.display.setCurrent(m_textBoxs[currentField]);
- }
- break;
-
- }
- }
-
- public void commandAction(Command c, Displayable d) {
- if (c == cmdOk) {
- this.m_strValues[currentField] = m_textBoxs[currentField]
- .getString();
- parent.display.setCurrent(this);
- } else
- parent.display.setCurrent(d);
-
- }
-
- private Alert alert = new Alert("信息", "", null, AlertType.INFO);
-
- private void sendSms() {
- if (m_strValues == null || m_strValues[0] == null
- || m_strValues[0].trim().length() != 11) {
- alert.setType(AlertType.ERROR);
- alert.setString("没有写信人号码啊");
- parent.display.setCurrent(alert);
- }
- Thread thread = new Thread(new Runnable() {
-
- public void run() {
-
- String address = "sms://" + m_strValues[0];
- MessageConnection mesconn = null;
- try {
- mesconn = (MessageConnection) Connector.open(address);
-
- TextMessage textmessage = (TextMessage) mesconn
- .newMessage(MessageConnection.TEXT_MESSAGE);
- textmessage.setAddress(address);
- textmessage.setPayloadText(m_strValues[1]);
- mesconn.send(textmessage);
- alert.setType(AlertType.INFO);
- alert.setString("发送成功");
- } catch (Throwable t) {
- t.printStackTrace();
- alert.setType(AlertType.ERROR);
- alert.setString("发送失败");
- } finally {
- try {
- mesconn.close();
- } catch (Exception ioe) {
- }
- }
- parent.display.setCurrent(alert);
- }
- });
-
- thread.start();
-
- }
-
- public void actionPerformed(MenuItemEvent e) {
- if (e.item == this.itemContent) {
- this.currentField = 1;
- parent.display.setCurrent(contentBox);
- } else {
- sendSms();
- }
-
- }
-
- }
原文地址:http://www.zhanglihai.com/blog/c_412.html
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
| 返回顶楼 |
|
|
- wuhua
- 等级:


- 性别:

- 文章: 554
- 积分: 1487
- 来自: 广州

|
哈哈,兄弟,我实现了一个输入法的功能,在我的UI组件里面
|
| 返回顶楼 |
|
|
- 超级莱鸟
- 等级:


- 性别:

- 文章: 19
- 积分: 120

|
呵呵,怎么实现的.用字库吗?
|
| 返回顶楼 |
|
|
- wuhua
- 等级:


- 性别:

- 文章: 554
- 积分: 1487
- 来自: 广州

|
是的。当然要用到字库了
|
| 返回顶楼 |
|
|
- huricha
- 等级: 初级会员

- 性别:

- 文章: 1
- 积分: 30
- 来自: 北京

|
谢谢 搂主的代码我也模仿做了一个例子
|
| 返回顶楼 |
|
|