📄 textfield.java
字号:
if (this.editField != null && text != this.text ) { Object bbLock = UiApplication.getEventLock(); synchronized (bbLock) { if (text != null) { this.editField.setText(text); } else { this.editField.setText(""); // setting null triggers an IllegalArgumentException } } } //#endif if (this.isPassword) { this.passwordText = text; if (text != null) { int length = text.length(); StringBuffer buffer = new StringBuffer( length ); for (int i = 0; i < length; i++) { buffer.append('*'); } text = buffer.toString(); } } //#ifdef tmp.directInput if (this.caretPosition == 0 && (text != null) && (this.text == null || this.text.length() == 0) ) { this.caretPosition = text.length(); this.caretColumn = this.caretPosition; //System.out.println("TextField.setString(): setting caretPosition to " + this.caretPosition + " for text [" + text + "]"); //TODO set caretX and caretY Positions and currentRowStart/currentRowEnd? } else if ( text != null && this.caretPosition > text.length()) { this.caretPosition = text.length(); this.caretColumn = this.caretPosition; } //#endif setText(text); //#ifdef tmp.directInput if ((text == null || text.length() == 0) && this.inputMode == MODE_FIRST_UPPERCASE) { this.nextCharUppercase = true; } //#endif } /** * 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 data is null * @see #setChars(char[], int, int) */ public int getChars(char[] data) { if (this.text == null) { return 0; } String txt = this.text; if (this.isPassword) { txt = this.passwordText; } char[] textArray = txt.toCharArray(); System.arraycopy(textArray, 0, data, 0, textArray.length ); return textArray.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 offset and length do not specify a valid range within the data array * @throws IllegalArgumentException - if data is illegal for the current input constraints * or if the text would exceed the current maximum capacity * @see #getChars(char[]) */ public void setChars(char[] data, int offset, int length) { char[] copy = new char[ length ]; System.arraycopy(data, offset, copy, 0, length ); setString( new String( copy )); } /** * 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 <CODE>getCaretPosition()</CODE> 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 String 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 input constraints * or if the insertion would exceed the current maximum capacity * @throws NullPointerException if src is null */ public void insert( String src, int position) { String txt = this.text; if (this.isPassword) { txt = this.passwordText; } String start = txt.substring( 0, position ); String end = txt.substring( position ); setString( start + src + end ); } /** * 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 <A HREF="../../../javax/microedition/lcdui/TextField.html#insert(java.lang.String, int)"><CODE>insert(String, int)</CODE></A>. * * <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 offset and length do not specify a valid range within the data array * @throws IllegalArgumentException - if the resulting contents would be illegal for the current input constraints * or if the insertion would exceed the current maximum capacity * @throws NullPointerException - if data is null */ public void insert(char[] data, int offset, int length, int position) { char[] copy = new char[ length ]; System.arraycopy( data, offset, copy, 0, length); insert( new String( copy ), 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 input constraints * @throws StringIndexOutOfBoundsException if offset and length do not specify a valid range within the contents of the TextField */ public void delete(int offset, int length) { String txt = this.text; if (this.isPassword) { txt = this.passwordText; } String start = txt.substring(0, offset ); String end = txt.substring( offset + length ); setString( start + end ); } /** * Returns the maximum size (number of characters) that can be * stored in this <code>TextField</code>. * * @return the maximum size in characters * @see #setMaxSize(int) */ public int getMaxSize() { return this.maxSize; } /** * 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 maxSize is zero or less. * or if the contents after truncation would be illegal for the current input constraints * @see #getMaxSize() */ public int setMaxSize(int maxSize) { if ((this.text != null && maxSize < this.text.length()) || (maxSize < 1)) { throw new IllegalArgumentException(); } //#if ! tmp.forceDirectInput && !polish.blackberry if (this.midpTextBox != null) { this.maxSize = this.midpTextBox.setMaxSize(maxSize); return this.maxSize; } else { //#endif this.maxSize = maxSize; return maxSize; //#if ! tmp.forceDirectInput && !polish.blackberry } //#endif } /** * Gets the number of characters that are currently stored in this * <code>TextField</code>. * * @return number of characters in the TextField */ public int size() { if (this.text == null) { return 0; } else { return this.text.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. * When the direct input mode is used, this method simply returns the current cursor position (= non blocking). * * @return the current caret position, 0 if at the beginning */ public int getCaretPosition() { //#ifdef tmp.allowDirectInput if (this.enableDirectInput) { return this.caretPosition; } else if (this.midpTextBox != null) { return this.midpTextBox.getCaretPosition(); } //# return 0; //#elif polish.blackberry //# return this.editField.getInsertPositionOffset(); //#elif tmp.forceDirectInput //# return this.caretPosition; //#else if (this.midpTextBox != null) { return this.midpTextBox.getCaretPosition(); } return 0; //#endif } /** * Sets the caret position. * Please note that this operation requires the direct input mode to work. * * @param position the new caret position, 0 puts the caret at the start of the line, getString().length moves the caret to the end of the input. */ public void setCaretPosition(int position) { //#if polish.blackberry this.editField.setCursorPosition(position); //#elif tmp.allowDirectInput || tmp.forceDirectInput if ( ! this.isInitialised ) { this.doSetCaretPosition = true; this.caretPosition = position; } else if (this.realTextLines == null ){ // ignore position when there is not text present } else { int row = 0; int col = 0; int passedCharacters = 0; String textLine = null; for (int i = 0; i < this.realTextLines.length; i++) { textLine = this.realTextLines[i]; passedCharacters += textLine.length(); //System.out.println("passedCharacters=" + passedCharacters + ", line=" + textLine ); if (passedCharacters >= position ) { row = i; col = textLine.length() - (passedCharacters - position); break; } } //#debug System.out.println("setCaretPosition, position=" + position + ", row=" + row + ", col=" + col ); this.caretRow = row; this.caretColumn = col; textLine = this.textLines[ row ]; this.originalRowText = textLine; String firstPart; if (this.caretColumn < textLine.length()) { firstPart = textLine.substring(0, this.caretColumn); if ( textLine.length() > 1 && textLine.charAt( textLine.length()-1) == '\n') { if ( textLine.length() - 1> this.caretColumn) { this.caretRowLastPart = textLine.substring( this.caretColumn, textLine.length() - 1 );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -