在之前的<J2me-UI设计(输入框)>一文中,由于时间关系没有增加输入法,今天老大抽查代码,狠狠心把这个功能补上
代码还没有优化,见谅
在原来的代码基础上添加:
如下
@Author :lipei
@Email-Addre :lipeijs@gmail.com
public static final String[] INPUT_MODE = {"ab","AB","12"};
private static byte iInputModeStatus = 0;
private static byte iOldKeyCode;
private byte iSelectedCharIndex;
private char iCurChars[]; //每个键的备选字母
private char iSelectedChar;
private static final byte INPUT_MODE_a = 0;
private static final byte INPUT_MODE_A = 1;
private static final byte INPUT_MODE_N = 2;
private StringBuffer iInputChars ;
private long startInputTime;
//输入超时时间
private int inputTimeOut = 800;
//得到转换后的键值
private int getNumberOfKey(int keyCode) {
if (keyCode > 47 && keyCode < 58)
return keyCode - 48;
else
return -1;
}
private void init() {
iOldKeyCode = -68;
startInputTime = 0;
iSelectedCharIndex = 0;
iCurChars=null;
iSelectedChar=0;
}
public void keyReleased(int keyCode) {
switch (iInputModeStatus) {
case INPUT_MODE_a:
case INPUT_MODE_A:
switch (keyCode) {
case Platform.KEY_NUM0:
break;
case Platform.KEY_NUM1:
break;
case Platform.KEY_LEFT:
break;
case Platform.KEY_RIGHT:
break;
case Platform.KEY_UP:
break;
case Platform.KEY_DOWN:
break;
case Platform.KEY_POUND:
break;
case Platform.KEY_STAR:
break;
default:
startInputTime = System.currentTimeMillis();
}
break;
}
}
private boolean isInputTimeOut() {
return System.currentTimeMillis() - startInputTime > inputTimeOut;
}
public void onClick(int keyCode) {
if (!hasFocus) //如果当前画面的焦点不在输入框,则不处理键盘输入
return;
if (keyCode == Platform.KEY_POUND) {
iInputModeStatus = ++iInputModeStatus > 2 ? 0 : iInputModeStatus;
init();
return;
}else if(keyCode == Platform.KEY_CLEAR)
{
deleteText();
return;
}else if (keyCode == Platform.KEY_ENTER){
changeDragDown();
return;
}
switch (iInputModeStatus) {
case INPUT_MODE_a:
case INPUT_MODE_A: //大小写输入法
iInputChars = new StringBuffer(text);
switch (keyCode) {
case Platform.KEY_NUM0:
case Platform.KEY_NUM1:
iInputChars.append('.');
break;
case Platform.KEY_LEFT:
break;
case Platform.KEY_RIGHT:
break;
case Platform.KEY_UP:
iItemFocus = iItemFocus > 0 ? (--iItemFocus) :
(iShowHistory.size() - 1);
break;
case Platform.KEY_STAR:
HttpRequest curRequest = new HttpRequest(text, HttpConnection.GET, null, null);
HttpWaitUI wait = new HttpWaitUI(curRequest.getUrl(),
new DataCanvas( ( (TextBoxCanvas)
canvas).getMIDlet()), ( (TextBoxCanvas) canvas).getMIDlet());
( (TextBoxCanvas) canvas).getMIDlet().getDisplay().setCurrent(wait);
break;
case Platform.KEY_DOWN:
iItemFocus = (iItemFocus + 1) % (iShowHistory.size());
break;
default:
int key = getNumberOfKey(keyCode);
if (key > 0) {
if (key != iOldKeyCode || isInputTimeOut()) {
iOldKeyCode = (byte) key;
iSelectedCharIndex = 0;
iCurChars = charLibrary.LETTER[key - 2];
iSelectedChar = iCurChars[iSelectedCharIndex];
if (iInputModeStatus == INPUT_MODE_a) {
iInputChars.append(iSelectedChar);
}
else {
iInputChars.append(Character.toUpperCase(iSelectedChar));
}
}
else if (key == iOldKeyCode && !isInputTimeOut()) {
iSelectedCharIndex = ++iSelectedCharIndex >
iCurChars.length - 1 ?
0 : iSelectedCharIndex;
iSelectedChar = iCurChars[iSelectedCharIndex];
if (iInputModeStatus == INPUT_MODE_a) {
iInputChars.setCharAt(iInputChars.length() - 1,
iSelectedChar);
}
else {
iInputChars.setCharAt(iInputChars.length() - 1,
Character.toUpperCase(iSelectedChar));
}
}
}
}
text = iInputChars.toString();
setText(text);
loadHistory(text);
iInputChars = null;
break;
case INPUT_MODE_N:
switch(keyCode){
case Platform.KEY_STAR:
addText(keyCode);
break;
case Platform.KEY_LEFT:
break;
case Platform.KEY_RIGHT:
break;
case Platform.KEY_UP:
iItemFocus = iItemFocus > 0 ? (--iItemFocus) :
(iShowHistory.size() - 1);
break;
case Platform.KEY_ENTER:
changeDragDown();
break;
case Platform.KEY_DOWN:
iItemFocus = (iItemFocus + 1) % (iShowHistory.size());
break;
default:
if (keyCode > 48 && keyCode < 58)
addText(keyCode);
break;
}
break;
}
}