⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 textfield.java

📁 有关j2me的很好的例子可以研究一下
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * @(#)TextField.java	1.164 01/08/09 * Copyright (c) 1999-2001 Sun Microsystems, Inc. All Rights Reserved. * * This software is the confidential and proprietary information of Sun * Microsystems, Inc. ("Confidential Information").  You shall not * disclose such Confidential Information and shall use it only in * accordance with the terms of the license agreement you entered into * with Sun. * * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING * THIS SOFTWARE OR ITS DERIVATIVES. */package javax.microedition.lcdui;import com.sun.midp.lcdui.EventHandler;/** * <P>A TextField is an editable text component that may be placed into * a {@link Form Form}. It can be * given a piece of text that is used as the initial value.</p> * * <P>A TextField has a maximum size, which is the maximum number of characters * that can be stored in the object at any time (its capacity). This limit is * enforced when the TextField instance is constructed, * when the user is editing text within the TextField, as well as * when the application program calls methods on the TextField that modify its * contents. The maximum size is the maximum stored capacity and is unrelated * to the number of characters that may be displayed at any given time. * The number of characters displayed and their arrangement into rows and * columns are determined by the device. </p> * * <p>The implementation may place a boundary on the maximum size, and the  * maximum size actually assigned may be smaller than the application had  * requested.  The value actually assigned will be reflected in the value * returned by {@link #getMaxSize() getMaxSize()}.  A defensively-written  * application should compare this value to the maximum size requested and be  * prepared to handle cases where they differ.</p> * * <a name="constraints"></a> * <h3>Input Constraints</h3> *  * <P>The TextField shares the concept of <em>input * constraints</em> with the {@link TextBox TextBox} object. The different * constraints allow the application to request that the user's input be * restricted in a variety of ways. The implementation is required to * restrict the user's input as requested by the application. For example, if * the application requests the NUMERIC constraint on a TextField, the * implementation must allow only numeric characters to be entered. </p> * * <P>The implementation is not required to do any syntactic validation * of the contents of the text object. Applications must be prepared * to perform such checking themselves.</P> * * <P>The implementation may provide special formatting for the value entered. * For example, a PHONENUMBER field may be separated and punctuated as * appropriate for the phone number conventions in use, grouping the digits * into country code, area code, prefix, etc. Any spaces or punctuation * provided are not considered part of the text field's value. For * example, a TextField with the PHONENUMBER constraint might display as * follows: <pre> * (408) 555-1212 * </pre> * but the value of the field visible to the application would be * a string representing a legal phone number like "4085551212". Note that * in some networks a '+' prefix is part of the number and returned as a part * of the string.</p> */public class TextField extends Item {    /**     * The user is allowed to enter any text.     *     * <P>Constant 0 is assigned to ANY.</P>     */    public final static int ANY = 0;    /**     * The user is allowed to enter an e-mail address.     *     * <P>Constant 1 is assigned to EMAILADDDR.</P>     */    public final static int EMAILADDR = 1;    /**     * The user is allowed to enter only an integer value. The implementation     * must restrict the contents to consist of an optional minus sign     * followed by an optional string of numerals.     *     * <P>Constant 2 is assigned to NUMERIC.</P>     */    public final static int NUMERIC = 2;    /**     * <P>The user is allowed to enter a phone number. The phone     * number is a special     * case, since a phone-based implementation may be linked to     * the native phone     * dialing application. The implementation may automatically start a phone     * dialer application that is initialized so that pressing a single key     * would be enough to make a call. The call must not made automatically     * without requiring user's confirmation.  The exact set of characters     * allowed is specific to the device and to the device's network and may     * include non-numeric characters.</P>     *     * <P>Constant 3 is assigned to PHONENUMBER.</P>     */    public final static int PHONENUMBER = 3;    /**     * The user is allowed to enter a URL.     *     * <P>Constant 4 is assigned to URL.</P>     */    public final static int URL = 4;    /**     * <P>The text entered must be masked so that the characters typed are not     * visible. The actual contents of the text field are not affected, but each     * character is displayed using a mask character such as "*". The character     * chosen as the mask character is implementation-dependent. This is useful     * for entering confidential information such as passwords or PINs (personal     * identification numbers). </P>     *     * <P>The PASSWORD modifier can be combined with other input constraints by     * using the logical OR operator (|). However, The PASSWORD modifier is     * nonsensical with some constraint values such as EMAILADDR,      * PHONENUMBER, and URL.</P>     *     * <P>Constant 0x10000 is assigned to PASSWORD.</P>     */    public final static int PASSWORD = 0x10000;    /**     * The mask value for determining the constraint mode. The      * application should     * use the logical AND operation with a value returned by      * getConstraints() and     * CONSTRAINT_MASK in order to retrieve the current constraint mode,     * in order to remove any modifier flags such as the PASSWORD flag.     *     * <P>Constant 0xFFFF is assigned to CONSTRAINT_MASK.</P>     */    public final static int CONSTRAINT_MASK = 0xFFFF;    /**     * The TextEditor to handle edits to this (and all) TextFields     */    final static TextEditor textEditor = new TextEditor();    /** The internal text handler */    TextBasic sD; /* shared data */    /** The overall width of this TextField */    private int width;    /** The label width of the label for this TextField */    private int labelWidth;    /** The width of the TextField */    private int tfWidth;    /** The content height of this TextField */    private int contentHeight;    /** The overall height of this TextField */    private int height;    /** A special spacer value */    private final static int SPACER = 8;    /** A flag indicating a horizontal layout */    private boolean horLayout;    /**     * Creates a new TextField object with the given label, initial     * contents, maximum size in characters, and constraints.     * If the text parameter is null, the TextField is created empty.     * The maxSize parameter must be greater than zero.     *     * @param label item label     * @param text the initial contents, or null if the TextField is to be empty     * @param maxSize the maximum capacity in characters     * @param constraints see <a href="#constraints">input constraints</a>     *     * @throws IllegalArgumentException if maxSize is zero or less     * @throws IllegalArgumentException if the value of the constraints     * parameter is invalid     * @throws IllegalArgumentException if text is illegal     * for the specified constraints     * @throws IllegalArgumentException if the length of the string exceeds     * the requested maximum capacity     * or the maximum capacity actually assigned     */    public TextField(String label, String text, int maxSize, int constraints) {        super(label, 1);        synchronized (Display.LCDUILock) {            sD = new TextBasic(constraints, maxSize, text);        }    }    /**     * Gets the contents of the TextField as a string value.     *     * @return the current contents     *     * @see #setString     */    public String getString() {        synchronized (Display.LCDUILock) {                return new String(sD.buffer, 0, sD.numChars);        }    }    /**     * Sets the contents of the TextField 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 the text is illegal     * for the current <a href="#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) {            int deltaHeight = sD.setString(text);            contentHeight += deltaHeight;            height        += deltaHeight;            contentChanged(0, 0, deltaHeight);        }    }    /**     * Copies the contents of the TextField 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) {            return sD.getChars(data);        }    }    /**     * Sets the contents of the TextField 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 null, the TextField     * is set to be empty and the other parameters are ignored.     *     * @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 the text is illegal     * for the current <a href="#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) {            int deltaHeight = sD.setChars(data, offset, length);            contentHeight += deltaHeight;            height        += deltaHeight;            contentChanged(0, 0, deltaHeight);        }    }    /**     * <p>Inserts a string into the contents of the TextField.  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 TextField.  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      * s to the current contents.</p>     *      * <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      * s 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 are 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) {            int deltaHeight = sD.insert(src, position);            contentHeight += deltaHeight;            height        += deltaHeight;            contentChanged(0, 0, deltaHeight);        }    }    /**     * <p>Inserts a subrange of an array of characters into the contents of     * the TextField.  The offset and length 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>     *      * @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     * 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -