J2me-UI设计(输入框)--续(英文输入法)

在之前的<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;

    }


  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值