📄 textfield.java
字号:
//# }
//#endif
if ( this.isPassword ) {
if (this.passwordText == null) {
return "";
}
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
//# 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 (text == null) {
//# this.caretPosition = 0;
//# } else if (this.caretPosition == 0
//# && (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 ( this.caretPosition > text.length()) {
//# this.caretPosition = text.length();
//# this.caretColumn = this.caretPosition;
//# }
//#endif
//#if tmp.updateDeleteCommand
updateDeleteCommand( text );
//#endif
setText(text);
//#ifdef tmp.directInput
//# if ((text == null || text.length() == 0) && this.inputMode == MODE_FIRST_UPPERCASE) {
//# this.nextCharUppercase = true;
//# }
//#endif
}
//#if tmp.updateDeleteCommand
private void updateDeleteCommand(String newText) {
// remove delete command when the caret is before the first character,
// add it when it is after the first character:
Screen scr = getScreen();
if ( scr != null && !this.isUneditable ) {
if ( newText == null
//#ifdef tmp.directInput
//# || this.caretPosition == 0
//#else
|| newText.length() == 0
//#endif
) {
scr.removeCommand( DELETE_CMD );
} else if ((this.text == null || this.text.length() == 0)
//#ifdef tmp.directInput
//# || this.caretPosition == 1
//#endif
) {
scr.addCommand( DELETE_CMD );
}
}
}
//#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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -