JavaMe连载(5)-绘制文本框TextEdit (十二)

2014-11-24 07:56:21 · 作者: · 浏览: 13
phics.drawLine(x+width,y,x+width,y+height);
}
}

3 实现光标闪烁

光标闪烁的实现需要用到线程,在UIController.java类中,需要绘制文本框的视图类,需要实现线程接口。

UIController.java

[html] 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_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;
}

UserRegist.java

[html] 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();
}

}
}
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();
}

}
}

4 调用高级界面TextBox子类PopUpTextBox

调用时,将调用对象名、编辑对象名、以及编辑框参数传递给PopUpTextBox对象(一定要有,目的是保存编辑框的值,否则多次调用返回时,不同编辑框的值被刷新为空了)

UserRegist.java(KeyPressed)

[html] 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_REGI