{
cursorBlinkOn1 = true;
cursorBlinkOn2 = false;
cursorBlinkOn3 = false;
currentlySelectedIndex =0;
}
else if(editor.equals("regist_passwd"))
{
cursorBlinkOn1 = false;
cursorBlinkOn2 = true;
cursorBlinkOn3 = false;
currentlySelectedIndex =1;
}
else if(editor.equals("regist_passwd_re"))
{
cursorBlinkOn1 = false;
cursorBlinkOn2 = false;
cursorBlinkOn3 = true;
currentlySelectedIndex =2;
}
//System.out.println(object_name);
//System.out.println(editor);
draw();
redraw();
}
public void draw()
{
//clearScreen();
backGroud.drawBackGroud(this, graphics);
head.drawHead(this,graphics,this.title);
menu.drawMenu(this,graphics,"下一步","退出");
drawBody();
}
private void redraw()
{
switch(currentlySelectedIndex)
{
case 0:
{
cursorBlinkOn2 = false;
cursorBlinkOn3 = false;
editor = "regist_name";
break;
}
case 1:
{
cursorBlinkOn1 = false;
cursorBlinkOn3 = false;
editor = "regist_passwd";
break;
}
case 2:
{
cursorBlinkOn1 = false;
cursorBlinkOn2 = false;
editor = "regist_passwd_re";
break;
}
default:;
}
textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);
textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);
textEdit_passwd.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);
textEdit_name.flushGraphics();
}
public void drawBody()
{
int margin =5;
ft = Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE);
String info = "用户名:\n";
String info_wrap1[] = StringDealMethod.format(info, width-10, ft);
graphics.setFont(ft);
graphics.setColor(Color.text);
for(int i=0; i<info_wrap1.length; i++)
{
graphics.drawString(info_wrap1[i],5, (i) * ft.getHeight()+40, Graphics.TOP|Graphics.LEFT);
}
textEdit_name_x = 5;
textEdit_name_y = info_wrap1.length * ft.getHeight()+40;
textEdit_name.drawTextBox(this, graphics, username, textEdit_name_x, textEdit_name_y, cursorBlinkOn1);
info = "用户密码:\n";
String info_wrap2[] = StringDealMethod.format(info, width-10, ft);
graphics.setFont(ft);
graphics.setColor(Color.text);
for(int i=0; i<info_wrap2.length; i++)
{
graphics.drawString(info_wrap2[i],5, (i+info_wrap1.length) * ft.getHeight()+textEdit_name.height+margin+40, Graphics.TOP|Graphics.LEFT);
}
textEdit_passwd_x = 5;
textEdit_passwd_y = (info_wrap1.length+info_wrap2.length) * ft.getHeight()+textEdit_name.height+margin+40;
textEdit_passwd.drawTextBox(this, graphics, passwd, textEdit_passwd_x, textEdit_passwd_y, cursorBlinkOn2);
info = "密码确认:\n";
String info_wrap3[] = StringDealMethod.format(info, width-10, ft);
graphics.setFont(ft);
graphics.setColor(Color.text);
for(int i=0; i<info_wrap3.length; i++)
{
graphics.drawString(info_wrap3[i],5, (i+info_wrap1.length+info_wrap2.length) * ft.getHeight()+textEdit_name.height+textEdit_passwd.height+2*margin+40, Graphics.TOP|Graphics.LEFT);
}
textEdit_passwd_re_x = 5;
textEdit_passwd_re_y = (info_wrap1.length+info_wrap2.length+info_wrap3.length) * ft.getHeight()+textEdit_name.height+textEdit_passwd.height+2*margin+40;
textEdit_passwd_re.drawTextBox(this, graphics, passwd_re, textEdit_passwd_re_x, textEdit_passwd_re_y, cursorBlinkOn3);
}
public void clearScreen()
{
graphics.setColor(0xff,0xff,0xff);
graphics.fillRect(0, 0, width, height);
}
public void checkTimeStamp()
{
long currentTime = System.currentTimeMillis();
//System.out.println("1");
if(lastCaretBlink + caretBlinkDelay < currentTime)
{
//System.out.println("2");
if(editor.equals("regist_name"))
{
cursorBlinkOn1 =! cursorBlinkOn1;
cursorBlinkOn2 = false;
cursorBlinkOn3 = false;
}
else if(editor.equals("regist_passwd"))
{
cursorBlinkOn1 = false;
cursorBlinkOn2 =! cursorBlinkOn2;
cursorBlinkOn3 = false;
}
else if(editor.equals("regist_passwd_re"))
{
cursorBlinkOn1 = false;
cursorBlinkOn2 = false;
cursorBlinkOn3 =! cursorBlinkOn3;
}
lastCaretBlink = currentTime;
}
}
public void run()
{
//System.out.println("run");
while(true)
{
checkTimeStamp();
redraw();
try
{
synchronized(this)
{
//System.out.println("3");
wait(50L);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
protected void keyPressed(int keyCode)
{
switch(keyCode)
{
case KeyID.SOFT_RIGHT:
{
controller.handleEvent(UIController.EventID.EVENT_EXIT,null);
break;
}
case KeyID.SOFT_LEFT:
{
if(username!="" && passwd!=""&&passwd_re!="")
{
if(passwd.equals(passwd_re))
{
userRecord.db_deleteAllRecord();
if(userRecord.db_getRecord(1)==null)
{
UserDataItem userItem = new UserDataItem(1,(username+","+passwd).getBytes());
userRecord.db_addRecord(userItem);
userItem = null;
System.gc();
}
String update = "start";
Object [] args = {"activeScreen", null, update};
controller.handleEvent(UIController.EventID.EVENT_NEXT_ACTIVE_SCREEN,args);
}
}
break;
}
case KeyID.KEY_EDIT:
case KEY_NUM0:
case KEY_NUM1:
case KEY_NUM2:
case KEY_NUM3:
case KEY_NUM4:
case KEY_NUM5:
case KEY_NUM6:
case KEY_NUM7:
case KEY_NUM8:
case KEY_NUM9:
{
//System.out.println(editor);
Object[] args = {object_name,editor,username,passwd,passwd_re};
controller.handleEvent(UIController.EventID.EVENT_USER_REGIST_EDIT,args);
break;
}
default:;
}
keyCode = getGameAction(keyCode);
switch(keyCode)
{
case UP:
case LEFT:
{
currentlySelectedIndex--;
if(currentlySelectedIndex<0)
{
currentlySelectedIndex=0;
}
else
{
redraw();
}
break;
}
case DOWN:
case RIGHT:
{
currentlySelectedIndex++;
if(currentlySelectedIndex>2)
{
currentlySelectedIndex=2;
}
else
{
redraw();
}
break;
}
}
}
}
*TextEdit是利用GameCanvas绘制的自定义文本编辑框。后续文章将给出具体实现。
【分析】
1 在MainMidlet调用控制器UIController,并向UIController传递midlet作为参数。
controller=new UIController(<span style="color:#ff0000;">this</span>);
try {
controller.init();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//
2 控制器的实现是通过一个事件的机制实现的, 在UIController中,先建立一个事件ID的内部类。
public static class EventID{
private EventID(){
}
public static final byte EVENT_EXIT = 0;//退出
public static final byte EVENT_NEXT_WELCOME_SCREEN = 1;//欢迎界面
public static final byte EVENT_NEXT_USER_REGIST_SCREEN = 2;//用户注册
public static final byte EVENT_USER_REGIST_EDIT = 3;//用户注册编辑
public static final byte EVENT_USER_REGIST_EDIT_BACK = 4;//用户注册编辑返回
public static final byte EVENT_NEXT_ACTIVE_SCREEN = 5;
//...
}
3 一次性初始化所有界面,分配内存,调用时,只是执行视图类的show方法。为什么不将初始话放在调用时进行呢?主要是因为视图类有多次重用,如果每一次调用都new(初始化,分配内存)一次,手机有限的内存很快会被用光,这是会出现一些程序自动退出的问题。
private void initObject()
{
welcomeScreen = new WelcomeScreen(this);
reg= new UserRegist(this);
activeScreen = new ActiveScreen(this);
...
textBox = new PopUpTextBox(this,"输入文本","", 1000, TextField.ANY);
}
4 控制器对视图的处理采用状态机实现
public void handleEvent( int eventID, Object[] args)
{
switch (eventID)
{
case EventID.EVENT_EXIT:
{
midlet.exit(false);
break;
}
case EventID.EVENT_NEXT_WELCOME_SCREEN:
{
welcomeScreen.show();
midlet.setCurrent(welcomeScreen);
break;
}
case EventID.EVENT_NEXT_USER_REGIST_SCREEN:
case EventID.EVENT_USER_REGIST_EDIT_BACK:
{
reg.show(args);
Thread thread = new Thread(reg);
thread.start();
midlet.setCurrent(reg);
break;
}
case EventID.EVENT_USER_REGIST_EDIT:
{
textBox.init(args);
midlet.setCurrent(textBox);
break;
}
case EventID.EVENT_NEXT_ACTIVE_SCREEN:
{
activeScreen.show(args);
Thread thread = new Thread(activeScreen);
thread.start();
midlet.setCurrent(activeScreen);
break;
}
//...
default:
break;
}
}
5 视图类初始化时,需要将控制器作为参数初始化,以对事件做出判断。如WelcomeScreen.java中先做出如下声明:
private UIController controller;
再在构造函数中,传递控制器
public WelcomeScreen(UIController control) {
// TODO Auto-generated constructor stub
super(false);
controller=control;
6 视图切换事件响应采用如下方式,在keyPressed中,对按键事件进行判断,然后调用UIController的handEvent方法。
protected void keyPressed(int keycode)
{
switch(keycode)
{
case KeyID.SOFT_RIGHT:
{
controller.handleEvent(UIController.EventID.EVENT_EXIT,null);
break;
}
case KeyID.SOFT_LEFT:
{
String editor = "regist_name";
Object [] args = {"registScreen",editor, null,null,null};
controller.handleEvent(UIController.EventID.EVENT_NEXT_USER_REGIST_SCREEN,args);
break;
}
default:;
}
}
}
7 控制器可以通过handEvent的args传递参数,如
String update = "start";
Object [] args = {"activeScreen", null, update};
controller.handleEvent(UIController.EventID.EVENT_NEXT_ACTIVE_SCREEN,args);
UserRegist传递了一个update变量给下一个视图。
8 在看一下模型,在UserDataItem中存储的是用户注册的信息。利用UserDataRecord类对记录进行操作。
在视图类中,通过以下方式调用:
先声明
private UserDataRecord userRecord;
构建对象
userRecord = new UserDataRecord();
使用对象
userRecord.db_deleteAllRecord();
if(userRecord.db_getRecord(1)==null)
{
UserDataItem userItem = new UserDataItem(1,(username+","+passwd).getBytes());
userRecord.db_addRecord(userItem);
userItem = null;
System.gc();
}
9 包com.token.view.components是对视图类中使用的自定义控件的封装
综述,这样就实现了模型、视图、控制的分离。