本文主要是介绍JavaMe 编程连载(5) - 绘制文本框TextEdit,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
【问题描述】在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)
这篇关于JavaMe 编程连载(5) - 绘制文本框TextEdit的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!