📄 textbox.java
字号:
package org.j4me.ui.components;
import javax.microedition.lcdui.*;
import org.j4me.ui.*;
/**
* A <code>TextBox</code> is a component for entering text, numbers, and keyboard data.
* It equivalent to the <code>javax.microedition.lcdui.TextField</code> class.
* <p>
* This component is not yet complete. Features that still need to be added
* include:
* <ul>
* <li>Suggestions
* <li>Constraint: EMAILADDR
* <li>Constraint: URL
* <li>Constraint: DECIMAL
* <li>Constraint: UNEDITABLE
* <li>Constraint: SENSITIVE
* <li>Constraint: NON_PREDICTIVE
* <li>Constraint: INITIAL_CAPS_WORD
* <li>Constraint: INITIAL_CAPS_SENTENCE
* </ul>
*
* @see javax.microedition.lcdui.TextField
*/
public class TextBox
extends Component
{
/**
* The default number of characters that can be entered into this text box component.
* The native edit screen (i.e. <code>javax.microedition.lcdui.TextBox</code>) allocates a
* buffer of this size so it cannot be too big to fit into memory.
*/
private static final int DEFAULT_MAX_CHARS = 128;
/**
* The number of pixels from the left, top, right, and bottom that
* text within the box is set.
*/
private static final int TEXT_OFFSET = 2;
/**
* The label that appears right above the text box. For example it might
* say "User Name". If this is <code>null</code> then no label will appear.
*/
private Label label;
/**
* The contents of the text box.
*/
private String contents;
/**
* The maximum number of characters that can be entered. If this is 0
* there is no limit.
*/
private int maxSize = DEFAULT_MAX_CHARS;
/**
* The constraints that define what kind of text is input. This matches
* the <code>TextField</code> input constraints integer.
* <p>
* One of the following types of text are possible:
* <ul>
* <li>ANY - (Default) Text, numbers, line breaks, and all data.
* <li>EMAILADDR - Email address.
* <li>NUMERIC - An integer.
* <li>PHONENUMBER - A phone number.
* <li>URL - A URL.
* <li>DECIMAL - A floating point number.
* </ul>
* <p>
* Additionally the input can be modified with a combination of any of:
* <ul>
* <li>PASSWORD
* <li>UNEDITABLE
* <li>SENSITIVE
* <li>NON_PREDICTIVE
* <li>INITIAL_CAPS_WORD
* <li>INITIAL_CAPS_SENTENCE
* </ul>
*
* @see javax.microedition.lcdui.TextField
*/
private int constraints = TextField.ANY;
/**
* Creates a new <code>TextBox</code> component.
*/
public TextBox ()
{
}
/**
* @return The text that appears above the text box. If
* <code>null</code> there is no text.
*/
public String getLabel ()
{
if ( label == null )
{
return null;
}
else
{
return label.getLabel();
}
}
/**
* @param label is the text that appears above the text box.
* If <code>null</code> there will be no text.
*/
public void setLabel (String label)
{
if ( label == null )
{
this.label = null;
}
else
{
if ( this.label == null )
{
this.label = new Label();
}
this.label.setLabel( label );
}
}
/**
* Gets the contents of the <code>TextBox</code> as a string value.
*
* @return The current contents or <code>null</code> if there is no data.
* @see #setString(String)
*/
public String getString ()
{
if ( contents == null )
{
return "";
}
else
{
return contents;
}
}
/**
* Sets the contents of the <code>TextBox</code> as a string value, replacing
* the previous contents.
*
* @param text is the new value of the <code>TextBox</code>, or <code>null<code> if
* the <code>TextBox</code> is to be made empty.
* @throws IllegalArgumentException if <code>text</code> is illegal for the
* current input constraints.
* @throws IllegalArgumentException if <code>text</code> would exceed the current
* maximum capacity.
*
* @see #getString()
*/
public void setString (String text)
{
this.contents = text;
}
/**
* Allows any text to be entered including letters, numbers, punctuation,
* and newlines.
*/
public void setForAnyText ()
{
setRestrictiveConstraint( TextField.ANY );
}
/**
* Allows only integer values to be input.
*/
public void setForNumericOnly ()
{
setRestrictiveConstraint( TextField.NUMERIC );
}
/**
* Allows for entering a phone number.
*/
public void setForPhoneNumber ()
{
setRestrictiveConstraint( TextField.PHONENUMBER );
}
/**
* @return <code>true</code> if this text box is for entering a phone number;
* <code>false</code> otherwise.
*/
public boolean isPhoneNumber ()
{
boolean phone = ((constraints & TextField.PHONENUMBER) != 0);
return phone;
}
/**
* Sets a restrictive constraint on this text box. Restrictive
* constraints define the type of data that can be entered. For
* example <code>TextField.ANY</code> or <code>TextField.NUMERIC</code>.
*
* @param restriction is the <code>TextField</code> restrictive constant.
*/
private void setRestrictiveConstraint (int restriction)
{
// Keep the modifiers.
constraints &= TextField.CONSTRAINT_MASK;
// Set the restrictive value.
constraints |= restriction;
}
/**
* Sets if this text box is for sensitive data or not. Sensitive data is
* displayed as '<code>*</code>' characters.
* <p>
* The default is for non-sensitive data.
*
* @param password is <code>true</code> if this text box contains a password;
* <code>false</code> otherwise.
*/
public void setPassword (boolean password)
{
setModifierConstraint( TextField.PASSWORD, password );
}
/**
* Returns if this text box is for sensitive data or not.
* <p>
* The default is for non-sensitive data.
*
* @return <code>true</code> if this text box contains a password; <code>false</code>
* otherwise.
*/
public boolean isPassword ()
{
boolean password = ((this.constraints & TextField.PASSWORD) != 0);
return password;
}
/**
* Sets a modifier constraint on this text box. Modifiers alter
* the type of text defined such as <code>TextField.PASSWORD</code>.
*
* @param restriction is the <code>TextField</code> modifier constant.
* @param on when <code>true</code> adds the modifier to the constraints
* and when <code>false</code> removes it.
*/
private void setModifierConstraint (int modifier, boolean on)
{
if ( on )
{
constraints |= modifier;
}
else
{
constraints &= ~modifier;
}
}
/**
* Returns the maximum size (number of characters) that can be stored
* in this <code>TextBox</code>.
*
* @return The maximum size in characters.
* @see #setMaxSize(int)
*/
public int getMaxSize ()
{
return maxSize;
}
/**
* Sets the maximum size (number of characters) that can be contained in
* this <code>TextBox</code>. If the current contents of the <code>TextBox</code> are
* larger than <code>maxSize</code>, the contents are truncated to fit.
*
* @param maxSize is the new maximum size. It must be 1 or more.
*/
public void setMaxSize (int maxSize)
{
if ( maxSize <= 0 )
{
throw new IllegalArgumentException( String.valueOf(maxSize) );
}
// Truncate if smaller than what exists already.
if ( maxSize < size() )
{
contents = contents.substring( 0, maxSize );
repaint();
}
this.maxSize = maxSize;
}
/**
* Gets the number of characters that are currently stored in this
* <code>TextBox</code>.
*
* @return The number of characters in the <code>TextBox</code>.
*/
public int size ()
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -