textfield.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,236 行 · 第 1/4 页
JAVA
1,236 行
setConstraintsImpl(constraints); // Create a LF with empty content itemLF = textFieldLF = LFFactory.getFactory().getTextFieldLF(this); // this will use inputClient // Right now setCharsImpl notifies LF a content change. // If LF is created as an absolutely last thing then // setCharsImple here does not need the notification. if (text == null) { setCharsImpl(null, 0, 0); } else { setCharsImpl(text.toCharArray(), 0, text.length()); } } } /** * Gets the contents of the <code>TextField</code> as a string value. * * @return the current contents * @see #setString */ public String getString() { synchronized (Display.LCDUILock) { textFieldLF.lUpdateContents(); return buffer.toString(); } } /** * Sets the contents of the <code>TextField</code> as a string * value, replacing the * previous contents. * * @param text the new value of the <code>TextField</code>, or * <code>null</code> if the TextField is to be made empty * @throws IllegalArgumentException if <code>text</code> * is illegal for the current * <a href="TextField.html#constraints">input constraints</a> * @throws IllegalArgumentException if the text would exceed the current * maximum capacity * @see #getString */ public void setString(String text) { synchronized (Display.LCDUILock) { if (text == null || text.length() == 0) { setCharsImpl(null, 0, 0); } else { setCharsImpl(text.toCharArray(), 0, text.length()); } } } /** * Copies the contents of the <code>TextField</code> into a * character array starting at * index zero. Array elements beyond the characters copied are left * unchanged. * * @param data the character array to receive the value * @return the number of characters copied * @throws ArrayIndexOutOfBoundsException if the array is too short for the * contents * @throws NullPointerException if <code>data</code> is <code>null</code> * @see #setChars */ public int getChars(char[] data) { synchronized (Display.LCDUILock) { textFieldLF.lUpdateContents(); try { buffer.getChars(0, buffer.length(), data, 0); } catch (IndexOutOfBoundsException e) { throw new ArrayIndexOutOfBoundsException(e.getMessage()); } return buffer.length(); } } /** * Sets the contents of the <code>TextField</code> from a * character array, replacing the * previous contents. Characters are copied from the region of the * <code>data</code> array * starting at array index <code>offset</code> and running for * <code>length</code> characters. * If the data array is <code>null</code>, the <code>TextField</code> * is set to be empty and the other parameters are ignored. * * <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 * @throws ArrayIndexOutOfBoundsException if <code>offset</code> * and <code>length</code> do not specify * a valid range within the data array * @throws IllegalArgumentException if <code>data</code> * is illegal for the current * <a href="TextField.html#constraints">input constraints</a> * @throws IllegalArgumentException if the text would exceed the current * maximum capacity * @see #getChars */ public void setChars(char[] data, int offset, int length) { synchronized (Display.LCDUILock) { setCharsImpl(data, offset, length); } } /** * 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) { synchronized (Display.LCDUILock) { // NullPointerException will be thrown by src.toCharArray insertImpl(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) { insertImpl(data, offset, length, 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) { deleteImpl(offset, length); } } /** * 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) { textFieldLF.lUpdateContents(); int oldCapacity = buffer.capacity(); if (oldCapacity == maxSize) { return maxSize; } buffer.setCapacity(maxSize); if (!textFieldLF.lValidate(buffer, constraints)) { buffer.setCapacity(oldCapacity); throw new IllegalArgumentException(); } // Notify LF that contents has changed due to maxSize textFieldLF.lSetMaxSize(maxSize); 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) { textFieldLF.lUpdateContents(); 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 textFieldLF.lGetCaretPosition();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?