【問題描述】在JavaMe連載(3)-也說MVC設計模式 一文中提到了一個TextEdit類,但沒有給出具體實現,TextEdit是采用GameCanvas繪制的文本編輯器。本文結合實例給出實現的方法。
【原理】
1 運用Graphics、GameCanvas繪制文本框和光標。
2 檢測到輸入事件時,跳轉到 高級界面->TextBox 。通過系統調用輸入法完成輸入。
3 將TextBox輸入的值返回給TextEdit對象。
【設計模式】
這個過程有點類似裝飾模式,實際上,實現輸入的還是TextBox,只是給TextBox裝飾了一下,形成了一個漂亮的外觀。
【代碼清單】
TextEdit.Java
- package com.token.vIEw.components;
- import Javax.microedition.lcdui.Font;
- import Javax.microedition.lcdui.Graphics;
- import Javax.microedition.lcdui.game.GameCanvas;
- public class TextEdit extends GameCanvas
- {
- private Font ft;
- public int width;
- public int height;
- public TextEdit(GameCanvas canvas)
- {
- super(false);
- }
- //繪制文本框
- public void drawTextBox(GameCanvas canvas, Graphics graphics, String text, int x, int y, boolean cursorBlinkOn)
- {
- //System.out.println("draw");
- int padding = 4;
- int margin = 2;
- ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
- graphics.setFont(ft);
- width = 3*canvas.getWidth()/5+2*padding;
- height = ft.getHeight()+2*padding;
- graphics.setColor(Color.frame);
- graphics.fillRect(x+1,y+1,width+margin,height+margin);
- graphics.setColor(Color.frameBg);
- graphics.drawRect(x, y,width, height);
- graphics.setColor(Color.background);
- graphics.fillRect(x+1, y+1,width-1,height-1);
- graphics.setColor(Color.text);
- graphics.drawString(text, x+padding, y+padding, Graphics.TOP|Graphics.LEFT);
- drawCursor(graphics, x+ft.stringWidth(text)+padding, y+padding, 1, ft.getHeight(), cursorBlinkOn);
- canvas.flushGraphics(x,y,width,height);
- }
- //繪制光標
- public void drawCursor(Graphics graphics, int x, int y, int width, int height, boolean cursorBlinkOn)
- {
- if(cursorBlinkOn)
- {
- ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_PLAIN,Font.SIZE_MEDIUM);
- graphics.setFont(ft);
- graphics.setColor(0x0,0x0,0x0);
- graphics.drawLine(x+width,y,x+width,y+height);
- }
- }
- }
PopUpTextBox.Java
- package com.token.vIEw;
- import Javax.microedition.lcdui.Command;
- import Javax.microedition.lcdui.CommandListener;
- import Javax.microedition.lcdui.Displayable;
- import Javax.microedition.lcdui.TextBox;
- import com.token.util.UIController;
- public class PopUpTextBox extends TextBox {
- private UIController controller;
- protected String canvasText = "";
- private Command okCommand;
- private Command cancelCommand;
- private String object_name = null;
- private String editor = null;
- private Object args_t[];
- private Object[] args;
- public PopUpTextBox(UIController control, String title, String text, int maxsize, int constraints)
- {
- super(title, text, maxsize, constraints);
- controller = control;
- args = new Object[6];
- okCommand = new Command("確定", Command.OK, 1);
- cancelCommand = new Command("取消", Command.CANCEL, 1);
- this.addCommand(okCommand);
- this.addCommand(cancelCommand);
- this.setCommandListener(new TextBoxListener());
- }
- public void init(Object[] args)
- {
- object_name = ((String)args[0]!=null)?(String)args[0]:"";
- editor = ((String)args[1]!=null)?(String)args[1]:"";
- //System.out.println(object_name);
- //System.out.println(editor);
- args_t = args;
- this.setString("");
- }
- protected void closeTextBox(boolean update) {
- if (update) canvasText = this.getString();
- //System.out.println(canvasText);
- if(object_name.equals("registScreen"))
- {
- if(editor.equals("regist_name"))
- {
- if(args_t[3]!=""||args_t[3]!=null||args_t[4]!=""||args_t[4]!=null)
- {
- args[0] = object_name;
- args[1] = editor;
- args[2] = this.canvasText;
- args[3] = args_t[3];
- args[4] = args_t[4];
- }
- controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);
- }
- else if(editor.equals("regist_passwd"))
- {
- if(args_t[2]!=""||args_t[2]!=null||args_t[4]!=""||args_t[4]!=null)
- {
- args[0] = object_name;
- args[1] = editor;
- args[2] = args_t[2];
- args[3] = this.canvasText;
- args[4] = args_t[4];
- }
- controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);
- }
- else if(editor.equals("regist_passwd_re"))
- {
- if(args_t[2]!=""||args_t[2]!=null||args_t[3]!=""||args_t[3]!=null)
- {
- args[0] = object_name;
- args[1] = editor;
- args[2] = args_t[2];
- args[3] = args_t[3];
- args[4] = this.canvasText;
- }
- controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT_BACK,args);
- }
- }
- //...
- }
- private class TextBoxListener implements CommandListener
- {
- public void commandAction(Command command, Displayable disp)
- {
- if(command==okCommand)
- {
- closeTextBox(true);
- }
- else if(command==cancelCommand)
- {
- closeTextBox(false);
- }
- }
- }
- }