📄 textfield.java
字号:
private ItemCommandListener additionalItemCommandListener; //#endif // this is outside of the tmp.directInput block, so that it can be referenced from the UiAccess class int inputMode; // the current input mode //#if tmp.directInput //#if tmp.supportsSymbolEntry private static List symbolsList; //#if polish.TextField.Symbols:defined //#= private static String definedSymbols = "${ escape(polish.TextField.Symbols)}"; //#else private static String definedSymbols = "@/\\<>().,-_:\""; //#endif //#ifdef polish.i18n.useDynamicTranslations private static Command ENTER_SYMBOL_CMD = new Command( Locale.get("polish.command.entersymbol"), Command.ITEM, 3 ); //#elifdef polish.command.entersymbol:defined //#= private static final Command ENTER_SYMBOL_CMD = new Command( "${polish.command.entersymbol}", Command.ITEM, 3 ); //#else //# private static final Command ENTER_SYMBOL_CMD = new Command( "Add Symbol", Command.ITEM, 3 ); //#endif //#endif //#ifdef polish.css.text-wrap private int currentXOffset; // used for scrolling to the correct position when text-wrapping is deactivated //#endif private boolean isKeyDown; //#ifdef polish.TextField.InputTimeout:defined //#= private static final int INPUT_TIMEOUT = ${polish.TextField.InputTimeout}; //#else private static final int INPUT_TIMEOUT = 1000; //#endif private static final int MODE_LOWERCASE = 0; private static final int MODE_FIRST_UPPERCASE = 1; // only the first character should be written in uppercase private static final int MODE_UPPERCASE = 2; static final int MODE_NUMBERS = 3; private static final int MODE_NATIVE = 4; //#ifdef polish.key.ChangeInputModeKey:defined //#= private static final int KEY_CHANGE_MODE = ${polish.key.ChangeInputModeKey}; //#else private static final int KEY_CHANGE_MODE = Canvas.KEY_POUND; //#endif private boolean nextCharUppercase; // is needed for the FIRST_UPPERCASE-mode private String[] realTextLines; // the displayed lines with spaces (which are otherwise removed) private int caretPosition; // the position of the caret in the text private int caretColumn; // the current column of the caret, 0 is the first column private int caretRow; // the current row of the caret, 0 is the first row private int caretX; private int caretY; private int caretWidth; private String originalRowText; //#ifdef polish.css.textfield-show-length private boolean showLength; //#endif private int lastKey; // the last key which has been pressed long lastInputTime; // the last time a key has been pressed private int characterIndex; // the index within the available characters of the current key // the characters for each key: //#ifdef polish.TextField.charactersKey1:defined //#= private static final String charactersKey1 = "${polish.TextField.charactersKey1}"; //#else private static final String charactersKey1 = ".,!?:/@_-+1"; //#endif //#ifdef polish.TextField.charactersKey2:defined //#= private static final String charactersKey2 = "${polish.TextField.charactersKey2}"; //#else private static final String charactersKey2 = "abc2"; //#endif //#ifdef polish.TextField.charactersKey3:defined //#= private static final String charactersKey3 = "${polish.TextField.charactersKey3}"; //#else private static final String charactersKey3 = "def3"; //#endif //#ifdef polish.TextField.charactersKey4:defined //#= private static final String charactersKey4 = "${polish.TextField.charactersKey4}"; //#else private static final String charactersKey4 = "ghi4"; //#endif //#ifdef polish.TextField.charactersKey5:defined //#= private static final String charactersKey5 = "${polish.TextField.charactersKey5}"; //#else private static final String charactersKey5 = "jkl5"; //#endif //#ifdef polish.TextField.charactersKey6:defined //#= private static final String charactersKey6 = "${polish.TextField.charactersKey6}"; //#else private static final String charactersKey6 = "mno6"; //#endif //#ifdef polish.TextField.charactersKey7:defined //#= private static final String charactersKey7 = "${polish.TextField.charactersKey7}"; //#else private static final String charactersKey7 = "pqrs7"; //#endif //#ifdef polish.TextField.charactersKey8:defined //#= private static final String charactersKey8 = "${polish.TextField.charactersKey8}"; //#else private static final String charactersKey8 = "tuv8"; //#endif //#ifdef polish.TextField.charactersKey9:defined //#= private static final String charactersKey9 = "${polish.TextField.charactersKey9}"; //#else private static final String charactersKey9 = "wxyz9"; //#endif //#ifdef polish.TextField.charactersKey0:defined //#= private static final String charactersKey0 = "${polish.TextField.charactersKey0}"; //#else private static final String charactersKey0 = " 0"; //#endif //#ifdef polish.TextField.charactersKeyStar:defined //#= private static final String charactersKeyStar = "${polish.TextField.charactersKeyStar}"; //#else private static final String charactersKeyStar = ".,!?:/@_-+"; //#endif //#ifdef polish.TextField.charactersKeyPound:defined //#= private static final String charactersKeyPound = "${polish.TextField.charactersKeyPound}"; //#else private static final String charactersKeyPound = null; //#endif private static final String[] CHARACTERS = new String[]{ charactersKey0, charactersKey1, charactersKey2, charactersKey3, charactersKey4, charactersKey5, charactersKey6, charactersKey7, charactersKey8, charactersKey9 }; private boolean caretPositionHasBeenSet; private boolean isNumeric; private boolean isDecimal; private boolean isEmail; private String caretRowFirstPart; private String caretRowLastPart; private int caretRowLastPartWidth; private int rowHeight; protected final Object lock; //#endif //#if polish.blackberry private PolishEditField editField; //#if polish.Bugs.ItemStateListenerCalledTooEarly private long lastFieldChangedEvent; //#endif //#endif private javax.microedition.lcdui.TextBox midpTextBox; protected boolean flashCaret = true; protected boolean isUneditable; private boolean doSetCaretPosition; /** * Creates a new <code>TextField</code> object with the given label, initial * contents, maximum size in characters, and constraints. * If the text parameter is <code>null</code>, the * <code>TextField</code> is created empty. * The <code>maxSize</code> parameter must be greater than zero. * An <code>IllegalArgumentException</code> is thrown if the * length of the initial contents string exceeds <code>maxSize</code>. * However, * the implementation may assign a maximum size smaller than the * application had requested. If this occurs, and if the length of the * contents exceeds the newly assigned maximum size, the contents are * truncated from the end in order to fit, and no exception is thrown. * * @param label item label * @param text the initial contents, or null if the TextField is to be empty * @param maxSize the maximum capacity in characters * @param constraints see input constraints * @throws IllegalArgumentException if maxSize is zero or less * or if the value of the constraints parameter is invalid * or if text is illegal for the specified constraints * or if the length of the string exceeds the requested maximum capacity */ public TextField( String label, String text, int maxSize, int constraints) { this( label, text, maxSize, constraints, null ); } /** * Creates a new <code>TextField</code> object with the given label, initial * contents, maximum size in characters, and constraints. * If the text parameter is <code>null</code>, the * <code>TextField</code> is created empty. * The <code>maxSize</code> parameter must be greater than zero. * An <code>IllegalArgumentException</code> is thrown if the * length of the initial contents string exceeds <code>maxSize</code>. * However, * the implementation may assign a maximum size smaller than the * application had requested. If this occurs, and if the length of the * contents exceeds the newly assigned maximum size, the contents are * truncated from the end in order to fit, and no exception is thrown. * * @param label item label * @param text the initial contents, or null if the TextField is to be empty * @param maxSize the maximum capacity in characters * @param constraints see input constraints * @param style the CSS style for this field * @throws IllegalArgumentException if maxSize is zero or less * or if the value of the constraints parameter is invalid * or if text is illegal for the specified constraints * or if the length of the string exceeds the requested maximum capacity */ public TextField( String label, String text, int maxSize, int constraints, Style style) { super( label, text, INTERACTIVE, style ); //#if tmp.directInput this.lock = new Object(); //#endif this.constraints = constraints; this.maxSize = maxSize; if (label != null) { this.title = label; } else { //#ifdef polish.title.input:defined //#= this.title = "${polish.title.input}"; //#else this.title = "Input"; //#endif } if ((constraints & PASSWORD) == PASSWORD) { this.isPassword = true; setString( text ); } //#ifndef polish.hasPointerEvents if ((constraints & NUMERIC) == NUMERIC && (constraints & DECIMAL) != DECIMAL) { this.enableDirectInput = true; } //#endif setConstraints(constraints); } //#if !polish.blackberry /** * Creates the TextBox used for the actual input mode. */ private void createTextBox() { String currentText = this.isPassword ? this.passwordText : this.text; this.midpTextBox = new javax.microedition.lcdui.TextBox( this.title, currentText, this.maxSize, this.constraints ); //TODO add i18n support this.midpTextBox.addCommand(StyleSheet.OK_CMD); if (!this.isUneditable) { this.midpTextBox.addCommand(StyleSheet.CANCEL_CMD); } this.midpTextBox.setCommandListener( this ); } //#endif /** * Gets the contents of the <code>TextField</code> as a string value. * * @return the current contents, an empty string when the current value is null. * @see #setString(java.lang.String) */ public String getString() { //#if tmp.directInput if (this.caretChar != this.editingCaretChar) { insertCharacter(); } //#endif //#if polish.blackberry if ( this.editField != null ) { return this.editField.getText(); } //#endif if ( this.isPassword ) { return this.passwordText; } else { if (this.text == null) { return ""; } return this.text; } } /** * Retrieves the decimal value entered with a dot as the decimal mark. * <ul> * <li>When the value has no decimal places it will be returned as it is: 12</li> * <li>When the value is null, null will be returned: null</li> * <li>When the value has decimal places, a dot will be used: 12.3</li> * </ul> * @return either the formatted value or null, when there was no input. * @throws IllegalStateException when the TextField is not DECIMAL constrained */ public String getDotSeparatedDecimalString() { //#if tmp.directInput //#if tmp.allowDirectInput if (this.enableDirectInput) { //#endif if (!this.isDecimal) { throw new IllegalStateException(); } String value = getString(); if ( Locale.DECIMAL_SEPARATOR == '.' || value == null) { return value; } else { return value.replace( Locale.DECIMAL_SEPARATOR, '.'); } //#if tmp.allowDirectInput } //#endif //#endif //#if !tmp.forceDirectInput if (( getConstraints() & DECIMAL)!= DECIMAL) { throw new IllegalStateException(); } String value = getString(); if (value == null) { return null; } return value.replace(',', '.'); //#endif } /** * Sets the contents of the <code>TextField</code> as a string * value, replacing the previous contents. * * @param text the new value of the TextField, or null if the TextField is to be made empty * @throws IllegalArgumentException if text is illegal for the current input constraints * or if the text would exceed the current maximum capacity * @see #getString() */ public void setString( String text) { //#debug System.out.println("TextField.setString( " + text + " )"); //#if !(tmp.forceDirectInput || polish.blackberry) if (this.midpTextBox != null) { this.midpTextBox.setString( text ); } //#endif //#if polish.blackberry
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -