📄 textfield.java
字号:
/* ------------------------------------------------------------------------- * Copyright 2004-2005 Nokia Corporation All rights reserved. Nokia Mobile Phones Restricted Rights: Use, duplication, or disclosure by the U.S. Government is subject to restrictions as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013, or in FAR 52.227-19, or in FAR 52.227-14 Alt. III, as applicable. This software is proprietary to and embodies the confidential technology of Nokia Possession, use, or copying of this software and media is authorized only pursuant to a valid written license from Nokia or an authorized sublicensor. Nokia - Wireless Software Solutions * ------------------------------------------------------------------------- */package samples.ui;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Graphics;/** * This class implements an editable text field which can hold and * display a single line of text. Its cursor can be scrolled left/right * through the text, and various input modes which control the set of * characters that appear when keys are pressed. An optional "password" * display mode masks out the text with asterisks. * <p> * Input modes: Alphanumeric, Numeric, Email Address, Username, ASCII * <p> * Display modes: Normal, Password */public class TextField extends Component { // Key codes /** Key code for the handset "left" button */ public static final int LEFT = -3; /** Key code for the handset "right" button */ public static final int RIGHT = -4; /** Key code for the handset "delete" button (if handset has a full keyboard) */ public static final int DELETE = -8; /** Key code for the handset "backspace" button (if handset has a full keyboard) */ public static final int BACKSPACE = 8; /** Key code for the handset "back" button */ public static final int BACK = -7; /** Key code for the handset "enter" button (if handset has a full keyboard) */ public static final int ENTER = 10; /** Key code for the handset "space" button (if handset has a full keyboard) */ public static final int SPACE = 32; /** Max. milliseconds between cycling keypresses. Longer than this and * a new character is produced */ public static final int REPEAT_DELTA = 600; /** Milliseconds between cursor blinks */ public static final int BLINK_RATE = 500; /** Milliseconds after which, if a user has been holding down a key, * character entered will be replaced with the number of the key pressed. */ public static final int HOLD_FOR_NUMBER_DUR = 1000; /** Milliseconds after which the last character entered in password * display mode will be replaced with an asterisk. */ public static final int MASK_PASSWORD_CHAR_DUR = 1500; /** * Alphanumeric entry mode. * These are the characters produced by cycling through presses * on various handset keys: * <ul> * <li><b>0</b> key: space ' ', and 0</li> * <li><b>1</b> key: . , ? ! 1 @ ' - _ ( ) : / * % # + < > =</li> * <li><b>2</b> key: a b c 2</li> * <li><b>3</b> key: d e f 3</li> * <li><b>4</b> key: g h i 4</li> * <li><b>5</b> key: j k l 5</li> * <li><b>6</b> key: m n o 6</li> * <li><b>7</b> key: p q r s 7</li> * <li><b>8</b> key: t u v 8</li> * <li><b>9</b> key: w x y z 9</li> * </ul> */ public static final int ENTRY_ALPHANUMERIC = 0; /** * Numeric mode. Numbers only (keys 0-9 produce numbers 0-9) */ public static final int ENTRY_NUMERIC = 1; /** * Email Address mode. Same as "Alphanumeric" mode, * with the following exceptions: * <ul> * <li><b>0</b> key: only 0</li> * <li><b>1</b> key: . 1 @ - _ +</li> * </ul> */ public static final int ENTRY_EMAILADDR = 2; /** * Username mode. Same as "Alphanumeric", with the * following exceptions: * <ul> * <li><b>0</b> key: only 0</li> * <li><b>1</b> key: 1 _</li> * </ul> */ public static final int ENTRY_USERNAME = 5; /** * Full ASCII (ASCII chars 32-126, inclusive). * Same as "Alphanumeric" mode, with the following exceptions: * <ul> * <li><b>1</b> key: . , ? ! 1 @ ' - _ ( ) : / * % # + < > = " $ & ; \ [ ] ^ ` | { } ~</li> * </ul> */ public static final int ENTRY_ASCII = 6; /** Displays TextField contents unhidden */ public static final int DISP_PLAINTEXT = 0; /** Displays TextField contents masked by cursors */ public static final int DISP_PASSWORD = 1; private static final int DISP_PASSWORD_ENTRY = 2; // Key cycle tables private static final String TABLE = " .adgjmptw"; private static final String TABLE_NUMERIC = "0123456789"; private static final String[] REPEAT_TABLE = { " 0", ".,?!1@'-_():/*%#+<>=", "abc2", "def3", "ghi4", "jkl5", "mno6", "pqrs7", "tuv8", "wxyz9", }; private static final String REPEAT_1KEY_EMAILADDR = ".1@-_+"; private static final String REPEAT_1KEY_USERNAME = "1_"; private static final String REPEAT_1KEY_ASCII = ".,?!1@'-_():/*%#+<>=\"$&;\\[]^`|{}~"; private int maxSize; private char[] charArray; private int cursor; private int nChars; private int cursorX; private int startChar; private int lastKey; private boolean keyHeld; private boolean showCursor; private int repeatCount; private int stringWidth; private long lastKeyPress; private boolean upperCase; private boolean qwerty; private int entryMode = ENTRY_ALPHANUMERIC; private int dispMode = DISP_PLAINTEXT; /** * Create a new TextField instance with the provided maxiumum character count. * * @param maxSize The maximum numbers of characters to accept. */ public TextField(int maxSize) { this.maxSize = maxSize; focusable = true; showCursor = true; qwerty = false; charArray = new char[maxSize+1]; nChars = 0; cursor = 0; stringWidth = 0; startChar = 0; } private char toUpper(char c) { if (upperCase && c >= 'a' && c <= 'z') c = (char)('A' + (c - 'a')); return c; } /** * Set the text for this instance. * * @param text The text for this instance. */ public void setText(String text) { nChars = cursor = Math.min(text.length(), maxSize); for (int i=0; i<cursor; i++) { charArray[i] = text.charAt(i); } stringWidth = font.charsWidth(charArray, 0, nChars); cursorX = font.charsWidth(charArray, 0, cursor); if (dispMode==DISP_PASSWORD_ENTRY) dispMode = DISP_PASSWORD; } /** * Get the text for this instance. * * @return The text for this instance. */ public String getText() { return new String( charArray, 0, nChars); } /** * Gets the entry mode for this instance * * @return The entry mode for this instance */ public int getEntryMode() { return entryMode; } /** * Sets the entry mode for this instance * * @param mode The entry mode for this instance */ public void setEntryMode( int mode) { this.entryMode = mode; } /** * Gets the display mode for this instance * * @return The display mode for this instance */ public int getDispMode() { return dispMode; } /** * Sets the display mode for this instance * * @param dispMode The display mode for this instance */ public void setDispMode(int dispMode) { this.dispMode = dispMode; } /** * Turns off "press-for-number" behavior if user was holding * down a key. * * @param action The Canvas action code of the key released * @param key The keycode of the key released */ public boolean keyReleased(int action, int key) { keyHeld = false; return true; } /** * Whenever the TextField gains focus, it starts an animation thread * which waits for timeouts to perform the following actions: * <ol> * <li>Blinks the cursor</li> * <li>Performs press-for-number behavior if user holds down a key</li> * <li>Masks out a recently entered character, if in password display mode</li> * </ol> * When the TextField loses focus, this thread exits. */ public void setFocus( boolean foc) { if (focus==foc) return; focus = foc; if (focus) { // TextField animation thread. Keeps track of: // 1) Cursor blinking // 2) If key is held down, decides when to turn last // entered character into a number. Thread focusThread = new Thread() { public void run() { long startTime = System.currentTimeMillis(); long lastBlink = startTime; while (focus) { try { Thread.sleep( 100); } catch (Exception e) {} long time = System.currentTimeMillis(); // Check for blink update if ((int)(time-lastBlink) > BLINK_RATE) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -