⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textfieldpopup.java

📁 J2me应用开发经典例子
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      public void run()
      {
		  System.out.println("CursorHandler run>>");
          resetCursorHandler();
          getComposer().caretRight();
          resetCursor();
      }
  }
  /**
   * The flag that determines whether or not this text component is editable.
   * If the flag is set to true, this text component becomes user editable.
   * If the flag is set to false, the user cannot change the text of this text component.
   */
  public boolean editable = true;
  /*
   * zhengyun 20051220 added
   */
  protected int lastKeyCode = 0;
  /**
   * Indicated if text component is in insert mode
   */
  volatile protected boolean insertMode = true;
  /**
   * Index into <code>charOptionCounter</code> for the last character used.
   */
  volatile protected int charOptionCounter = 0;
  /**
   * The characters, mapped to the number keys
   */
  final static protected char[][] CHAR_OPTIONS = {
      {'0', '+'},
      {'1', '.', '-', '\'', '+', '!', '\"', '*', '#', '$', '%', '(', ')', '@', '&', '_',
       '~', '`', '^', '=', '\\', '|', ',', '/', '?', '<', '>', ':', ';', '[', ']'},
      {'A', 'B', 'C', 'a', 'b', 'c', '2'},
      {'D', 'E', 'F', 'd', 'e', 'f', '3'},
      {'G', 'H', 'I', 'g', 'h', 'i', '4'},
      {'J', 'K', 'L', 'j', 'k', 'l', '5'},
      {'M', 'N', 'O', 'm', 'n', 'o', '6'},
      {'P', 'Q', 'R', 'S', 'p', 'q', 'r', 's', '7'},
      {'T', 'U', 'V', 't', 'u', 'v', '8'},
      {'W', 'X', 'Y', 'Z', 'w', 'x', 'y', 'z', '9'},
      {' '}
  };
  /**
   * Phone keys used for data input.
   */
  final static protected int[] KEY_OPTIONS = {
      PlatformCanvas.KEY_NUM0, PlatformCanvas.KEY_NUM1, PlatformCanvas.KEY_NUM2, PlatformCanvas.KEY_NUM3,
      PlatformCanvas.KEY_NUM4, PlatformCanvas.KEY_NUM5, PlatformCanvas.KEY_NUM6, PlatformCanvas.KEY_NUM7,
      PlatformCanvas.KEY_NUM8, PlatformCanvas.KEY_NUM9, PlatformCanvas.KEY_POUND
  };
  protected boolean preProcessKeyCode(int keyCode) {

      if (keyCode != lastKeyCode) {
          insertMode = true;
          charOptionCounter = 0;
          return true;
      }
      return false;
  }
  protected char processKeyCode(int keyCode) {
	  System.out.println("processKeyCode>>" + keyCode);
	  
      final int keyIndex = getKeyOption(keyCode);

      if (keyIndex == -1) {
          insertMode = true;
          charOptionCounter = 0;
          return (char) keyCode;
      }

      int rc = charOptionCounter;
      final int len = CHAR_OPTIONS[keyIndex].length;
      if (rc >= len) rc %= len;

      char ch, rslt = 0;
      for (int p = 0; p < len; p++) {
          ch = CHAR_OPTIONS[keyIndex][rc];
		  System.out.println("processKeyCode CHAR_OPTIONS>>" + ch);
          if (isCharValid(ch)) {
              if (rslt == 0) {
                  charOptionCounter = rc + 1;
                  rslt = ch;
              } else {
                  insertMode = false;
                  return rslt;
              }
          }
          rc = (rc + 1) % len;
      }

      insertMode = true;
      charOptionCounter = 0;
      return rslt;
  }
  /**
   * Numeric characters
   */
  protected static final String NUMERIC_CHARS = "0123456789";

  /**
   * Uppercase characters
   */
  protected static final String UC_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

  /**
   * Lowercase characters
   */
  protected static final String LC_CHARS = "abcdefghijklmnopqrstuvwxyz";

  /**
   * Email special characters
   */
  protected static final String EMAIL_SPEC_CHARS = "~_.@";

  /**
   * Space character
   */
  protected static final String SPACE_CHARS = " ";

  /**
   * All special character
   */
  protected static final String ALL_SPEC_CHARS = " ~`!@#$%^&*()_-+=\\|'\".,/?<>:;[]";

  /**
   * Do not allows any chars
   */
  public static final int C_NO_CHARS = 0x00; 

  /**
   * Allows numbers in the document
   * @see #NUMERIC_CHARS
   */
  public static final int C_NUMERIC = 0x01; 

  /**
   * Allows lowercase letters in the document
   * @see #LC_CHARS
   */
  public static final int C_LOWER_CASE = 0x02;

  /**
   * Allows uppercase letters in the document
   * @see #UC_CHARS
   */
  public static final int C_UPPER_CASE = 0x04;

  /**
   * Allows email special letters in the document
   * @see #EMAIL_SPEC_CHARS
   */
  public static final int C_EMAIL_SPEC_CHARS = 0x08;

  /**
   * Allows spaces in the document
   * @see #SPACE_CHARS
   */
  public static final int C_SPACE = 0x10;

  /**
   * Allows all special characters in the document
   * @see #ALL_SPEC_CHARS
   */
  public static final int C_ALL_SPEC_CHARS = 0x80;

  /**
   * Allows all alphabet characters and space in the document
   */
  public static final int C_ALPHA = C_LOWER_CASE | C_UPPER_CASE | C_SPACE;

  /**
   * Allows alphanumeric characters in the document
   */
  public static final int C_ALPHA_NUMERIC = C_ALPHA | C_NUMERIC;

  /**
   * Allows all email characters in the document
   */
  public static final int C_EMAIL =
          C_ALPHA_NUMERIC | C_EMAIL_SPEC_CHARS ^ C_SPACE;

  /**
   * Allows any characters in the document
   */
  public static final int C_ANY = 0xFF;
  /**
   * Constraints for the document
   */
  protected int constraint = C_NO_CHARS;
  /**
   * The array of allowed characted
   */
  protected char[] validChars = null;
  /**
   * Creates a new <code>TextComponent</code> instance.
   * @param constr Constraints for this document
   */
  public void initValidChars(int constr) {
      this.constraint = constr;

      int vc = 0;
      if ((constr & C_NUMERIC) > 0) {
          vc += NUMERIC_CHARS.length();
      }
      if ((constr & C_LOWER_CASE) > 0) {
          vc += UC_CHARS.length();
      }
      if ((constr & C_UPPER_CASE) > 0) {
          vc += LC_CHARS.length();
      }
      if ((constr & C_ALL_SPEC_CHARS) > 0) {
          vc += ALL_SPEC_CHARS.length();
      } else {
          if ((constr & C_EMAIL_SPEC_CHARS) > 0) {
              vc += EMAIL_SPEC_CHARS.length();
          }
          if ((constr & C_SPACE) > 0) {
              vc += SPACE_CHARS.length();
          }
      }

      validChars = new char[vc];
      int i, len;
      vc = 0;
      if ((constr & C_NUMERIC) > 0) {
          len = NUMERIC_CHARS.length();
          for (i = 0; i < len; i++)
              validChars[vc++] = NUMERIC_CHARS.charAt(i);
      }
      if ((constr & C_LOWER_CASE) > 0) {
          len = LC_CHARS.length();
          for (i = 0; i < len; i++)
              validChars[vc++] = LC_CHARS.charAt(i);
      }
      if ((constr & C_UPPER_CASE) > 0) {
          len = UC_CHARS.length();
          for (i = 0; i < len; i++)
              validChars[vc++] = UC_CHARS.charAt(i);
      }
      if ((constr & C_ALL_SPEC_CHARS) > 0) {
          len = ALL_SPEC_CHARS.length();
          for (i = 0; i < len; i++)
              validChars[vc++] = ALL_SPEC_CHARS.charAt(i);
      } else {
          if ((constr & C_EMAIL_SPEC_CHARS) > 0) {
              len = EMAIL_SPEC_CHARS.length();
              for (i = 0; i < len; i++)
                  validChars[vc++] = EMAIL_SPEC_CHARS.charAt(i);
          }
          if ((constr & C_SPACE) > 0) {
              len = SPACE_CHARS.length();
              for (i = 0; i < len; i++)
                  validChars[vc++] = SPACE_CHARS.charAt(i);
          }
      }
  }
  /**
   * Checks if the character is allows symbol
   * @param ch test character
   * @return <code>true</code> if the characted is allwed symbol
   * for the component, <code>false</code> otherwise
   */
  protected boolean isCharValid(char ch) {
	  System.out.println("isCharValid>>" + ch);
	  
      int vcl = validChars.length;
      for (int i = 0; i < vcl; i++) {
          if (validChars[i] == ch) return true;
      }
      return false;
  }
  protected int getKeyOption(int keyCode) {
	  System.out.println("getKeyOption>>" + keyCode);
      int l = KEY_OPTIONS.length;
      for (int keyIndex = 0; keyIndex < l; keyIndex++) {
          if (KEY_OPTIONS[keyIndex] == keyCode) return keyIndex;
      }
      return -1;
  }
  protected int calculatedCaretPosition;
  protected int calculatedCursorOffset;
  protected int currentLength;
  /**
   * The echo character, which is used when
   * the user wishes to disguise the characters
   * typed into the text field.
   * The disguises are removed if echoChar = <code>0</code>.
   */
  public char echoChar = 0;
  public int adjustment = 0;
  protected static final boolean SCROLLABLE = false;
  protected char[] getFormattedText(boolean calcCursorOffset) {
	  char[] formattedText;
      currentLength = composer.getCurrentLength();
      if (echoChar != 0) {
          formattedText = new char[currentLength];
          for (int i = 0; i < currentLength; i++) {
              formattedText[i] = echoChar;
          }
      } else {
          formattedText = composer.getChars();
      }
      if (calcCursorOffset) {
          calculatedCaretPosition = composer.getCaretPosition();
          calculatedCursorOffset = getCursorOffset(formattedText, calculatedCaretPosition);
      }
      return formattedText;
  }
  /**
   * The gap between the screen border and the textfield frame
   */
  final static public int BORDER_WIDTH = 1;
  /**
   * The gap between the textfield frame and textfield text
   */
  final static public int BORDER_GAP = 1;
  /**
  * The total vertical gap for textfield text
  */
 protected final static int VOFFSET = BORDER_WIDTH + BORDER_GAP + Style.V_GAP;
 /**
  * The total horizontal gap for textfield text
  */
 protected final static int HOFFSET = BORDER_WIDTH + BORDER_GAP + Style.H_GAP;
 /**
  * Default text size
  */
 static public final int DEFAULT_SIZE = 10;
  /**
   * Returns the cursor offset (in pixels) for the composed string
   */
  public int getCursorOffset(char[] text, int caretPosition) {

      int offset = HOFFSET - adjustment;
      if (echoChar == 0) {
          return offset + m_font.charsWidth(text, 0, caretPosition);
      } else {
          return offset + m_font.charWidth(echoChar) * caretPosition;
      }
  }
  /////////////////////////////////////////////////////////////////////////
  
  
  /**
   * Disposes all resources held by this popup and closes it.
   */
  public void dispose()
  {
    m_active = false;
    m_text = null;
    m_altTexts = null;
    m_listener = null;
    m_breakTextData = null;
    System.gc();
  }

  /**
   * Returns whether this popup is active or not.
   * 
   * @return true if active, false otherwise.
   */
  public boolean isActive()
  {
    return m_active;
  }

  /**
   * Returns alternative index on timeout
   * 
   * @return timeout alternative
   */
  public byte getTimeOutChoice()
  {
    return m_timeOutAlt;
  }

  /**
   * Called by framework to check if popup reached its' timeout.
   * 
   * @return true if timeout, false otherwise.
   */
  protected boolean pollTimeout()
  {
    if (m_active)
    {
      if (m_endTime > 0 && System.currentTimeMillis() > m_endTime)
      {
        m_active = false;
        if (m_listener != null)
        {
          m_listener.selectedChoice(m_timeOutAlt, true);
          return true;
        }
      }
    }
    return false;
  }

  // Runnable impl to poll this popup
  public void run()
  {
    while (isActive())
    {
      // Poll popup timeout
      try
      {
        Thread.sleep(1000);
        pollTimeout();
      } catch (InterruptedException e) {}
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -