📄 textfield.java
字号:
lastBlink = time; showCursor = !showCursor; repaint(); } // Check for hold-for-number update (flip character to number // if key held down for long enough) if (keyHeld && lastKeyPress>0 && cursor>0 && (int)(time-lastKeyPress) > HOLD_FOR_NUMBER_DUR && lastKey >= Canvas.KEY_NUM0 && lastKey <= Canvas.KEY_NUM9 ) { charArray[cursor-1] = TABLE_NUMERIC.charAt( lastKey-Canvas.KEY_NUM0); stringWidth = font.charsWidth( charArray, 0, nChars); cursorX = font.charsWidth( charArray, 0, cursor); keyHeld = false; lastKeyPress = time; repaint(); } // Check for masking unmasked (freshly entered) characters in PASSWORD mode if ((int)(time-lastKeyPress) > MASK_PASSWORD_CHAR_DUR && dispMode == DISP_PASSWORD_ENTRY && cursor>0 ) { dispMode = DISP_PASSWORD; repaint(); } } } }; focusThread.start(); } } /** * Update the state of this instance in light of a key press action. * * @param action The type of action. The TextField class ignores * the action parameter, but responds to a wide variety * of key types corresponding to text entry events. * @param key The key released. * @return true if the instance handled the event, and * false if the instance ignored the event. */ public boolean keyPressed(int action, int key) { boolean handled = false; int delta = 0; // Check if we're using a QWERTY keyboard intead of number pad if (key == SPACE || key == BACKSPACE || (key >= 'a' && key <= 'z')) qwerty = true; // Check for key-cycling, if last press happened recently enough, and // we're in an entry mode that has key-cycling. delta = (int)(System.currentTimeMillis() - lastKeyPress); boolean repeat = entryMode != ENTRY_NUMERIC && // Don't repeat in NUMERIC mode delta < REPEAT_DELTA && // Don't repeat if last press was too long ago key == lastKey && // Don't repeat if key pressed is different than last one !qwerty && // Don't repeat if using QWERTY keyboard !(key==Canvas.KEY_NUM0 && // Don't repeat on 0 key in USERNAME or (entryMode==ENTRY_USERNAME || // EMAILADDR entry modes entryMode==ENTRY_EMAILADDR)); if (dispMode==DISP_PASSWORD_ENTRY) dispMode=DISP_PASSWORD; // Left / Right if (key == Canvas.LEFT || key == LEFT || key == Canvas.RIGHT || key == RIGHT) { if (key == Canvas.LEFT || key == LEFT) { if (cursor>0) cursor--; if (cursor<startChar) startChar = cursor; } else if (key == Canvas.RIGHT || key == RIGHT) { if (cursor<nChars) cursor++; } // Update start char and cursor pos stringWidth = font.charsWidth( charArray, startChar, nChars-startChar); cursorX = font.charsWidth( charArray, startChar, cursor-startChar); while (cursorX > getWidth()-textOffsetX-2) { startChar++; cursorX = font.charsWidth(charArray,startChar,cursor-startChar); } repaint(); handled = true; } // Pound else if (key == Canvas.KEY_POUND) { upperCase = !upperCase; handled = true; repaint(); } // Delete / Backspace else if (key == DELETE || key == BACKSPACE) { if (cursor > 0) deleteChar( cursor-1); handled = true; repaint(); } // Fire / Enter else if (key == Canvas.FIRE || key == ENTER) { notifyListeners(new Event(this, Event.TEXT_CHANGED, new String(charArray, 0, cursor))); handled = true; } // All other keys - either cycle through current key's character options, // or place a new character in the text field. else { handled = enterCharacter( repeat, key); } lastKey = key; keyHeld = true; lastKeyPress = System.currentTimeMillis(); return handled; } /** * Handles entry of a character from the keyboard or keypad. * The character entered depends on the entry mode, and * keypad cycling behavior (if the time interval since the * last keypress was recent enough, then "repeat" will be * true and the last entered character will cycle once. * Otherwise, a new character will be inserted at the * cursor position.) * * @param repeat <code>true</code> if last key entered should * cycle * @param key code of key entered * @return <code>true</code> if keypressed handled */ private boolean enterCharacter( boolean repeat, int key) { String tableEntry = ""; char c = ' '; boolean handled = false; // REPEAT: // Use repeat tables for cycling through keys (all modes except NUMERIC) if (repeat && key >= Canvas.KEY_NUM0 && key <= Canvas.KEY_NUM9) { // Use different "1" key repeat tables, depending on entry mode if (key == Canvas.KEY_NUM1) { switch (entryMode) { case ENTRY_ALPHANUMERIC:tableEntry = REPEAT_TABLE[1]; break; case ENTRY_EMAILADDR: tableEntry = REPEAT_1KEY_EMAILADDR; break; case ENTRY_USERNAME: tableEntry = REPEAT_1KEY_USERNAME; break; case ENTRY_ASCII: tableEntry = REPEAT_1KEY_ASCII; break; } // For ALPHANUMERIC, EMAILADDR, and DOMAIN modes, keys other than "1" key all // share the same repeat tables. } else { tableEntry = REPEAT_TABLE[ key - Canvas.KEY_NUM0]; } c = tableEntry.charAt(++repeatCount % tableEntry.length()); setChar( c, cursor-1); handled = true; repaint(); // NEW keypress: // Pick first entry in current mode's repeat tables, for given key pressed } else { repeatCount = 0; if (cursor <= maxSize && ( key == SPACE || (key >= 'a' && key <= 'z') || (key >= Canvas.KEY_NUM0 && key <= Canvas.KEY_NUM9))) { // For "1" key, use first char in repeat table for current entry mode if (key == Canvas.KEY_NUM1) { switch (entryMode) { case ENTRY_ALPHANUMERIC:c = REPEAT_TABLE[1].charAt(0); break; case ENTRY_NUMERIC: c = TABLE_NUMERIC.charAt(1); break; case ENTRY_EMAILADDR: c = REPEAT_1KEY_EMAILADDR.charAt(0);break; case ENTRY_USERNAME: c = REPEAT_1KEY_USERNAME.charAt(0); break; case ENTRY_ASCII: c = REPEAT_1KEY_ASCII.charAt(0); break; } // For all other keys: pick chars either from numeric or alphanumeric // first-char tables } else { // NUMERIC mode - keys display their digit. // USERNAME and EMAILADDR modes - zero key only displays '0', never 'space' characer. if (entryMode == ENTRY_NUMERIC || (entryMode==ENTRY_USERNAME && key==Canvas.KEY_NUM0) || (entryMode==ENTRY_EMAILADDR && key==Canvas.KEY_NUM0) ) c = qwerty ? (char)key : TABLE_NUMERIC.charAt(key - Canvas.KEY_NUM0); // For all other modes use ALPHANUMERIC's beginning character else c = qwerty ? (char)key : TABLE.charAt(key - Canvas.KEY_NUM0); } insertChar( c, cursor); handled = true; repaint(); } } return handled; } /** * Inserts a character at a given location in the string * * @param c The character to insert * @param loc The index where it should be inserted. */ private void insertChar( char c, int loc) { if (nChars >= maxSize) return; if (loc<0 || loc>=maxSize) return; for (int i=nChars; i>loc; i--) charArray[i] = charArray[i-1]; nChars++; cursor++; setChar( c, loc); } /** * Sets a character at a given location in the string * * @param c The character to set * @param loc The index of the character to be overwritten with 'c' */ private void setChar( char c, int loc) { if (loc<0 || loc>=maxSize) return; c = toUpper(c); charArray[ loc] = c; if (dispMode==DISP_PASSWORD) dispMode=DISP_PASSWORD_ENTRY; stringWidth = font.charsWidth( charArray, startChar, nChars-startChar); cursorX = font.charsWidth( charArray, startChar, cursor-startChar); while (cursorX > getWidth()-textOffsetX-2) { startChar++; cursorX = font.charsWidth(charArray,startChar,cursor-startChar); } } /** * Removes a character at a given location in the string. The * string will be compacted to close the gap. * @param loc The index where it should be removed. */ private void deleteChar( int loc) { if (loc<0 || loc>=maxSize) return; for (int i=loc; i<nChars-1; i++) charArray[i] = charArray[i+1]; cursor--; nChars--; if (startChar>0) startChar--; stringWidth = font.charsWidth( charArray, startChar, nChars-startChar); cursorX = font.charsWidth( charArray, startChar, cursor-startChar); if (dispMode==DISP_PASSWORD_ENTRY) dispMode=DISP_PASSWORD; } /** * Paint this instance. * * @param g The Graphics object to use for painting operations. */ public void paint(Graphics g) { int yoff; super.paint(g); yoff = (height - font.getHeight()) / 2 + textOffsetY;// g.setFont(font); int color = (focus && focusFontColor != -1) ? focusFontColor : fontColor; g.setColor( color); char[] chars = charArray; // Blank out most or all of the password with asterisks if display // mode is type PASSWORD. if ((dispMode==DISP_PASSWORD || dispMode==DISP_PASSWORD_ENTRY) && nChars>0) { chars = new char[nChars+1]; for (int i=0; i<nChars; i++) chars[i] = '*'; // Leave most recently entered character unmasked if user has just entered it. if (dispMode==DISP_PASSWORD_ENTRY && focus && cursor>0) chars[cursor-1] = charArray[cursor-1]; chars[nChars] = 0; stringWidth = font.charsWidth( chars, startChar, nChars-startChar); cursorX = font.charsWidth( chars, startChar, cursor-startChar); } g.setClip( x, y, getWidth()-1, getHeight()); // clip tight for text// g.drawChars( chars, startChar, nChars-startChar, x + textOffsetX, y + yoff, NW_ANCHOR); font.drawChars( g, chars, startChar, nChars-startChar, x + textOffsetX, y + yoff); // Draw cursor if text field has focus, and cursor is in "blink on" if (focus && showCursor) { g.drawLine( x + cursorX + textOffsetX, y + 2, x + cursorX + textOffsetX, y + height - 3 ); } g.setClip( x-1, y-1, width+3, height+3); // clip wide for border/shadow paintBorder(g); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -