📄 textfield.java
字号:
} else { if (offset < 0 || offset > data.length || length < 0 || length > data.length || offset + length < 0 || offset + length > data.length) { throw new ArrayIndexOutOfBoundsException(); } if (length > buffer.capacity()) { throw new IllegalArgumentException(); } if (length > 0) { DynamicCharacterArray dca = new DynamicCharacterArray(length); dca.set(data, offset, length); if (!TextPolicy.isValidString(dca, inputClient.getConstraints())) { throw new IllegalArgumentException(); } } buffer.set(data, offset, length); cursor.index = buffer.length(); cursor.option = Text.PAINT_USE_CURSOR_INDEX; } invalidate(); } } /** * Inserts a string into the contents of the * <code>TextField</code>. The string is * inserted just prior to the character indicated by the * <code>position</code> parameter, where zero specifies the first * character of the contents of the <code>TextField</code>. If * <code>position</code> is * less than or equal to zero, the insertion occurs at the beginning of * the contents, thus effecting a prepend operation. If * <code>position</code> is greater than or equal to the current size of * the contents, the insertion occurs immediately after the end of the * contents, thus effecting an append operation. For example, * <code>text.insert(s, text.size())</code> always appends the string * <code>s</code> to the current contents. * * <p>The current size of the contents is increased by the number of * inserted characters. The resulting string must fit within the current * maximum capacity. </p> * * <p>If the application needs to simulate typing of characters it can * determining the location of the current insertion point * ("caret") * using the with {@link #getCaretPosition() getCaretPosition()} method. * For example, * <code>text.insert(s, text.getCaretPosition())</code> inserts the string * <code>s</code> at the current caret position.</p> * * @param src the <code>String</code> to be inserted * @param position the position at which insertion is to occur * * @throws IllegalArgumentException if the resulting contents * would be illegal for the current * <a href="TextField.html#constraints">input constraints</a> * @throws IllegalArgumentException if the insertion would exceed * the current * maximum capacity * @throws NullPointerException if <code>src</code> is <code>null</code> */ public void insert(String src, int position) { insert(src.toCharArray(), 0, src.length(), position); } /** * Inserts a subrange of an array of characters into the contents of * the <code>TextField</code>. The <code>offset</code> and * <code>length</code> parameters indicate the subrange * of the data array to be used for insertion. Behavior is otherwise * identical to {@link #insert(String, int) insert(String, int)}. * * <p>The <code>offset</code> and <code>length</code> parameters must * specify a valid range of characters within * the character array <code>data</code>. * The <code>offset</code> parameter must be within the * range <code>[0..(data.length)]</code>, inclusive. * The <code>length</code> parameter * must be a non-negative integer such that * <code>(offset + length) <= data.length</code>.</p> * * @param data the source of the character data * @param offset the beginning of the region of characters to copy * @param length the number of characters to copy * @param position the position at which insertion is to occur * * @throws ArrayIndexOutOfBoundsException if <code>offset</code> * and <code>length</code> do not specify * a valid range within the <code>data</code> array * @throws IllegalArgumentException if the resulting contents * would be illegal for the current * <a href="TextField.html#constraints">input constraints</a> * @throws IllegalArgumentException if the insertion would exceed * the current * maximum capacity * @throws NullPointerException if <code>data</code> is <code>null</code> */ public void insert(char[] data, int offset, int length, int position) { synchronized (Display.LCDUILock) { int pos = buffer.insert(data, offset, length, position); if (!TextPolicy.isValidString(buffer, inputClient.getConstraints())) { buffer.delete(pos, length); throw new IllegalArgumentException(); } if (position <= cursor.index) { cursor.index += length; cursor.option = Text.PAINT_USE_CURSOR_INDEX; } invalidate(); } } /** * Insert a single character at the given position * * @param ch character to insert * @param position position to insert */ private void insert(char ch, int position) { char chArray[] = { ch }; insert(chArray, 0, 1, position); } /** * Deletes characters from the <code>TextField</code>. * * <p>The <code>offset</code> and <code>length</code> parameters must * specify a valid range of characters within * the contents of the <code>TextField</code>. * The <code>offset</code> parameter must be within the * range <code>[0..(size())]</code>, inclusive. * The <code>length</code> parameter * must be a non-negative integer such that * <code>(offset + length) <= size()</code>.</p> * * @param offset the beginning of the region to be deleted * @param length the number of characters to be deleted * * @throws IllegalArgumentException if the resulting contents * would be illegal for the current * <a href="TextField.html#constraints">input constraints</a> * @throws StringIndexOutOfBoundsException if <code>offset</code> * and <code>length</code> do not * specify a valid range within the contents of the <code>TextField</code> */ public void delete(int offset, int length) { synchronized (Display.LCDUILock) { if (length == 0) { return; } String str = buffer.toString(); buffer.delete(offset, length); if (!TextPolicy.isValidString(buffer, inputClient.getConstraints())) { buffer.delete(0, buffer.length()); buffer.insert(0, str); throw new IllegalArgumentException(); } if (cursor.index >= offset) { int diff = cursor.index - offset; cursor.index -= (diff < length) ? diff : length; cursor.option = Text.PAINT_USE_CURSOR_INDEX; } invalidate(); } } /** * Returns the maximum size (number of characters) that can be * stored in this <code>TextField</code>. * @return the maximum size in characters * @see #setMaxSize */ public int getMaxSize() { synchronized (Display.LCDUILock) { return buffer.capacity(); } } /** * Sets the maximum size (number of characters) that can be contained * in this * <code>TextField</code>. If the current contents of the * <code>TextField</code> are larger than * <code>maxSize</code>, the contents are truncated to fit. * * @param maxSize the new maximum size * * @return assigned maximum capacity - may be smaller than requested. * @throws IllegalArgumentException if <code>maxSize</code> is zero or less. * @throws IllegalArgumentException if the contents * after truncation would be illegal for the current * <a href="TextField.html#constraints">input constraints</a> * @see #getMaxSize */ public int setMaxSize(int maxSize) { synchronized (Display.LCDUILock) { int oldCapacity = buffer.capacity(); buffer.setCapacity(maxSize); if (!TextPolicy.isValidString(buffer, inputClient.getConstraints())) { buffer.setCapacity(oldCapacity); throw new IllegalArgumentException(); } invalidate(); return buffer.capacity(); } } /** * Gets the number of characters that are currently stored in this * <code>TextField</code>. * @return number of characters in the <code>TextField</code> */ public int size() { synchronized (Display.LCDUILock) { return buffer.length(); } } /** * Gets the current input position. For some UIs this may block and ask * the user for the intended caret position, and on other UIs this may * simply return the current caret position. * * @return the current caret position, <code>0</code> if at the beginning */ public int getCaretPosition() { synchronized (Display.LCDUILock) { return cursor.index; } } /** * Sets the input constraints of the <code>TextField</code>. If * the the current contents * of the <code>TextField</code> do not match the new * <code>constraints</code>, the contents are * set to empty. * * @param constraints see <a href="#constraints">input constraints</a> * * @throws IllegalArgumentException if constraints is not any of the ones * specified in <a href="TextField.html#constraints">input constraints</a> * @see #getConstraints */ public void setConstraints(int constraints) { synchronized (Display.LCDUILock) { if ((constraints & CONSTRAINT_MASK) < ANY || (constraints & CONSTRAINT_MASK) > DECIMAL) { throw new IllegalArgumentException(); } if (inputClient.setConstraints(constraints)) { if (!TextPolicy.isValidString(buffer, constraints)) { delete(0, buffer.length()); } } // saved the editable state editable = !((UNEDITABLE & constraints) == UNEDITABLE); multiLine = (constraints & CONSTRAINT_MASK) != PHONENUMBER; invalidate(); } } /** * Gets the current input constraints of the <code>TextField</code>. * * @return the current constraints value (see * <a href="#constraints">input constraints</a>) * @see #setConstraints */ public int getConstraints() { synchronized (Display.LCDUILock) { return inputClient.getConstraints(); } } /** * Sets a hint to the implementation as to the input mode that should be * used when the user initiates editing of this <code>TextField</code>. The * <code>characterSubset</code> parameter names a subset of Unicode * characters that is used by the implementation to choose an initial * input mode. If <code>null</code> is passed, the implementation should * choose a default input mode. * * <p>See <a href="#modes">Input Modes</a> for a full explanation of input * modes. </p> * * @param characterSubset a string naming a Unicode character subset, * or <code>null</code> * * @since MIDP 2.0 */ public void setInitialInputMode(String characterSubset) { synchronized (Display.LCDUILock) { initialInputMode = characterSubset; } } // ======================================================================== // package private methods // ======================================================================== /** * Called to commit any pending character from the input handler */ void commitPendingInteraction() { inputHandler.endComposition(false); } /** * Determine if this Item should have a newline after it * * @return true if it should have a newline after */ boolean equateNLA() { if (super.equateNLA()) { return true; } return ((layout & Item.LAYOUT_2) != Item.LAYOUT_2); } /** * Determine if this Item should have a newline before it * * @return true if it should have a newline before */ boolean equateNLB() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -