Package totalcross.ui

Class Edit

  • All Implemented Interfaces:
    TimerListener, TextControl
    Direct Known Subclasses:
    KeyboardEdit

    public class Edit
    extends Control
    implements TextControl, TimerListener
    Edit is a text entry control.

    Here is an example showing an edit control being used:

     public class MyProgram extends MainWindow
     {
        Edit edit;
    
        public void initUI()
        {
           edit = new Edit();
           add(edit,LEFT,TOP);
        }
     
    Important: if you wish to open a popup window after a FOCUS_OUT event has occured, you must open the window with popupNonBlocking, never with popup. Otherwise, the window will be openned twice. Here's a sample code on how to proceed:
     public class Test extends MainWindow
     {
          Edit ed;
          MessageBox mb;
    
          public void initUI()
          {
             add(ed = new Edit(""), LEFT, CENTER);
             add(new Button("btn"), LEFT, AFTER+5);
          }
    
          public void onEvent(Event event)
          {
             switch (event.type)
             {
                case ControlEvent.FOCUS_OUT:
                   if (event.target == ed)
                   {
                      event.consumed = true; // this is important!
                      (mb=new MessageBox("Hi","Verinha")).popupNonBlocking();
                   }
                   break;
                case ControlEvent.WINDOW_CLOSED:
                   if (event.target == mb)
                      ed.setText("Window closed.");
                   break;
             }
          }
       }
     
    A long click on an Edit will result in a menu with copy/paste options to be displayed.
    See Also:
    clipboardDelay
    • Field Detail

      • prefH

        public static final int prefH
      • hasFocus

        protected boolean hasFocus
      • editable

        protected boolean editable
        Specifies if the control accepts input from the user. Note: do not change this directly; use the setEditable method instead.
        See Also:
        setEditable(boolean)
      • overwrite

        public boolean overwrite
        Specifies if new chars should overwrite existing ones.
      • alignment

        public int alignment
        Sets the alignment of this Edit, which can be LEFT (default), CENTER or RIGHT. Note that it will always edit at left, but on focus lost, it will drawn aligned.
        Since:
        SuperWaba 5.03
      • caption

        public java.lang.String caption
        The caption to draw when this Edit is empty. In Material UI, you must set the caption BEFORE you add this control to the container; also remember to set the height to at least PREFERRED, it is 75% taller.
        See Also:
        captionColor
      • captionColor

        public int captionColor
        The caption's color.
      • captionIcon

        public Image captionIcon
        An optional caption's icon
      • useNativeNumericPad

        public static boolean useNativeNumericPad
        Set to true on Android devices to use the native numeric pad when mode is set to CURRENCY. Note that the numeric keybaord will probably appear only on the default keyboard.
      • hasBorder

        protected boolean hasBorder
      • isMaskedEdit

        protected boolean isMaskedEdit
      • showKeyboardOnNextEvent

        public boolean showKeyboardOnNextEvent
      • copyStr

        public static java.lang.String copyStr
        Used to inform that a copy operation has been made. You can localize this message if you wish.
      • cutStr

        public static java.lang.String cutStr
        Used to inform that a cut operation has been made. You can localize this message if you wish.
      • pasteStr

        public static java.lang.String pasteStr
        Used to inform that a paste operation has been made. You can localize this message if you wish.
      • replaceStr

        public static java.lang.String replaceStr
        Used to inform that a replace operation has been made. You can localize this message if you wish.
      • commandStr

        public static java.lang.String commandStr
        Used to inform that a command operation has been made. You can localize this message if you wish.
      • canMoveFocus

        protected boolean canMoveFocus
        Set to false to disable focus change on this Edit
      • captionPress

        public CaptionPress captionPress
        Handler for the CaptionPress
      • optionalValue4CalculatorBox

        public java.lang.String optionalValue4CalculatorBox
        Defines an optional value to be used in the CalculatorBox when the keyboard type is KBD_NUMERIC or KBD_CALCULATOR. Replaces the decimal separator / 00 char.
        Since:
        TotalCross 1.5
      • keyboardTitle

        public java.lang.String keyboardTitle
        Defines a title that can be used in the Keyboards.
        Since:
        TotalCross 1.53
      • clipboardDelay

        public static int clipboardDelay
        Defines the time that the user will have to press to see a popup menu with copy/paste options. Set to -1 to disable it; defaults to 1500 (1.5 seconds) in pen devices, -1 in finger devices (because in these devices . Also affects MultiEdit.
        Since:
        TotalCross 1.3
      • validChars

        protected java.lang.String validChars
      • keyboard

        public static VirtualKeyboard keyboard
        The KeyboardBox used in all Edits.
      • calendar

        public static CalendarBox calendar
        The CalendarBox used in all Edits.
      • calculator

        public static CalculatorBox calculator
        The CalculatorBox used in all Edits.
      • numeric

        public static CalculatorBox numeric
        The NumericBox used in all Edits.
      • time

        public static TimeBox time
        The TimeBox used in all Edits.
      • mode

        protected byte mode
      • maxLength

        protected int maxLength
      • capitalise

        public byte capitalise
        Sets the capitalise settings for this Edit. Text entered will made as is, uppercase or lowercase
        See Also:
        ALL_NORMAL, ALL_UPPER, ALL_LOWER
      • removeFocusOnAction

        public static boolean removeFocusOnAction
      • focusColor

        public int focusColor
        Color to apply to the Edit when it has focus (works only on Android user interface style). By default, there's only a blinking cursor.
        Since:
        TotalCross 1.3
      • useNumericBoxInsteadOfCalculator

        public static boolean useNumericBoxInsteadOfCalculator
        Use the NumericBox instead of the Calculator in all Edits that have mode equal to CURRENCY. Note that you can set for each control by calling ed.setKeyboard(Edit.KBD_NUMERIC).
        Since:
        TotalCross 1.3
      • kbdType

        protected byte kbdType
      • hasCursorWhenNotEditable

        public boolean hasCursorWhenNotEditable
        Set to false if you don't want the cursor to blink when the edit is not editable
      • autoSelect

        public boolean autoSelect
        If set to true, the text will be auto-selected when the focus enters. True by default on penless devices.
      • selectLast

        public boolean selectLast
        Keep selection of last character
      • persistentSelection

        public boolean persistentSelection
        Keep the selection persistent; otherwise, it is reset if you change the letter
      • KBD_NONE

        public static final byte KBD_NONE
        No keyboard will be popped up for this Edit
        See Also:
        Constant Field Values
      • KBD_DEFAULT

        public static final byte KBD_DEFAULT
        The default keyboard for the current mode will be used for this Edit
        See Also:
        Constant Field Values
      • KBD_KEYBOARD

        public static final byte KBD_KEYBOARD
        The Keyboard class (or the internal virtual keyboard) will be used for this Edit
        See Also:
        Constant Field Values
      • KBD_CALCULATOR

        public static final byte KBD_CALCULATOR
        The Calculator will be used for this Edit
        See Also:
        Constant Field Values
      • KBD_CALENDAR

        public static final byte KBD_CALENDAR
        The Calendar will be used for this Edit
        See Also:
        Constant Field Values
      • KBD_NUMERIC

        public static final byte KBD_NUMERIC
        The NumericBox will be used for this Edit
        See Also:
        Constant Field Values
      • KBD_TIME

        public static final byte KBD_TIME
        The TimeBox will be used for this Edit
        See Also:
        Constant Field Values
      • numbersSet

        public static final java.lang.String numbersSet
        to be used in the setValidChars method
        See Also:
        Constant Field Values
      • currencyCharsSet

        public static final java.lang.String currencyCharsSet
        to be used in the setValidChars method
        See Also:
        Constant Field Values
      • dateSet

        public static final java.lang.String dateSet
        to be used in the setValidChars method
      • NORMAL

        public static final byte NORMAL
        to be used in the setMode method
        See Also:
        Constant Field Values
      • CURRENCY

        public static final byte CURRENCY
        to be used in the setMode method
        See Also:
        Constant Field Values
      • PASSWORD

        public static final byte PASSWORD
        to be used in the setMode method. The last char will be always shown
        See Also:
        Constant Field Values
      • PASSWORD_ALL

        public static final byte PASSWORD_ALL
        to be used in the setMode method. All chars are replaced by '*'
        See Also:
        Constant Field Values
      • ALL_NORMAL

        public static final byte ALL_NORMAL
        to be used in the capitalise property
        See Also:
        Constant Field Values
      • ALL_UPPER

        public static final byte ALL_UPPER
        to be used in the capitalise property
        See Also:
        Constant Field Values
      • ALL_LOWER

        public static final byte ALL_LOWER
        to be used in the capitalise property
        See Also:
        Constant Field Values
      • virtualKeyboard

        public boolean virtualKeyboard
        Defines if this Edit can behave as with virtual keyboard or not.
        Since:
        TotalCross 1.6
      • cursorThickness

        public static int cursorThickness
        Cursor thickness
      • pushedInsertPos

        protected int pushedInsertPos
      • pushedStartSelectPos

        protected int pushedStartSelectPos
      • pushedxOffset

        protected int pushedxOffset
    • Constructor Detail

      • Edit

        public Edit()
        Construct an Edit with FILL as preferred width. Note that you cannot use RIGHT or CENTER at the x coordinate if you use this constructor.
      • Edit

        public Edit​(java.lang.String mask)
        Construct an Edit with the default width computed based in the specified mask and in the control's font. In order to allow the mask to be used as a real mask, you must call the setMode method. If mask is "", the FILL width is choosen.
        See Also:
        setMode(byte, boolean)
    • Method Detail

      • mapKeys

        public void mapKeys​(java.lang.String from,
                            java.lang.String to)
        Maps the keys in the from char array into the keys in the to char array. For example enable a 'numeric pad' on devices that has the 1 in the u character, you can use this:
         ed.mapKeys("uiojklnm!.","1234567890");
         
        To make sure that lowercase characters are also handled, you should also change the capitalise mode:
         ed.capitalise = Edit.ALL_LOWER;
         
        If you want to disable a set of keys, use the setValidChars method. Note that mapKeys have precendence over setValidChars.
        Parameters:
        from - The source keys. Must have the same length of to. Set to null to disable mapping.
        to - The destination keys. Must have the same length of from
        Since:
        TotalCross 1.01
        See Also:
        setValidChars(String)
      • setDecimalPlaces

        public void setDecimalPlaces​(int count)
        Set the number of decimal placed if a masked edit with CURRENCY mode. Default is 2 decimal places. It cannot be used with masked Edits; pass the number of decimal places in the mask itself. The only exception is when you want to use the default CURRENCY mask, passing a null mask in the constructor; in this situation, you can call setDecimalPlaces before calling setMode, and a mask will be constructed with the given number of decimals.
      • getDecimalPlaces

        public int getDecimalPlaces()
        Returns the number of decimal places.
      • onFontChanged

        protected void onFontChanged()
        Description copied from class: Control
        Called after a setFont
        Overrides:
        onFontChanged in class Control
      • getMask

        public java.lang.String getMask()
        Returns the mask passed on the constructor.
      • setMode

        public void setMode​(byte mode)
        Used to set the valid characters that can be entered by using one of the mode constants, without masking. To enable masking the input, you have to call the setMode method passing the mode and true as parameter. Note that setMode calls setKeyboard(KBD_DEFAULT), so be sure to set the mode before calling setKeyboard.
        See Also:
        NORMAL, DATE, CURRENCY, PASSWORD, PASSWORD_ALL
      • getMode

        public int getMode()
        Return the current mode.
        Since:
        TotalCross 1.27
      • setMode

        public void setMode​(byte mode,
                            boolean maskedEdit)
        Used to set the valid characters that can be entered by using one of the mode constants, optionally enabling the mask to be applied to the input. Note that setMode calls setKeyboard(KBD_DEFAULT), so be sure to set the mode before calling setKeyboard.
        See Also:
        NORMAL, DATE, CURRENCY, PASSWORD, PASSWORD_ALL
      • getDefaultCurrencyMask

        public static char[] getDefaultCurrencyMask​(int decimalPlaces)
      • setValidChars

        public void setValidChars​(java.lang.String validCharsString)
        Sets the valid chars that can be entered in this edit (they are converted to uppercase to make the verification easy). if null is passed, any char can be entered. The chars are case insensitive. If you pass "" (empty string), no chars will be able to be inputted, and movement, delete and copy/paste operations will also be disabled.
        See Also:
        mapKeys(java.lang.String, java.lang.String)
      • isCharValid

        protected boolean isCharValid​(char c)
        Return true if the given char exists in the set of valid characters for this Edit
      • setMaxLength

        public void setMaxLength​(int length)
        Sets the desired maximum length for text entered in the Edit. Does nothing if the edit has a mask.
        Since:
        SuperWaba 2.0 beta 4
      • getMaxLength

        public int getMaxLength()
      • pushPosState

        protected void pushPosState()
      • popPosState

        protected void popPosState()
      • charPos2x

        protected int charPos2x​(int n)
      • getText

        public java.lang.String getText()
        Returns the text displayed in the edit control. If masking is enabled, the text with the mask is returned; to get the text without the mask, use the getTextWithoutMask method.
        Specified by:
        getText in interface TextControl
        See Also:
        getTextWithoutMask()
      • getTextWithoutMask

        public java.lang.String getTextWithoutMask()
        Returns the text without the mask. For non-currency mode, only chars whose corresponding mask is '9' are returned.
        See Also:
        getText()
      • getTextBuffer

        public java.lang.StringBuffer getTextBuffer()
        Returns the text's buffer. Do NOT change the buffer contents, since changing it will not affect the char widths array, thus, leading to a wrong display.
        Since:
        TotalCross 1.0
      • setText

        public void setText​(java.lang.String s,
                            boolean postPressed)
      • setText

        public void setText​(java.lang.String s)
        Sets the text displayed in the edit control. If you're setting the text in CURRENCY mode, the text must be set not formatted (unmasked).
        Specified by:
        setText in interface TextControl
      • setEditable

        public void setEditable​(boolean on)
        Sets if the control accepts input from the user. If set to false, you must explicitly call the clear method of this edit.
      • isEditable

        public boolean isEditable()
        Gets if the control accepts input from the user
      • onBoundsChanged

        protected void onBoundsChanged​(boolean screenChanged)
        Description copied from class: Control
        Called after a setRect.
        Overrides:
        onBoundsChanged in class Control
        Parameters:
        screenChanged - If the bounds were changed due to a screen change (rotation, collapse)
      • getPreferredWidth

        public int getPreferredWidth()
        Description copied from class: Control
        Returns the preferred width of this control.
        Overrides:
        getPreferredWidth in class Control
      • getPreferredHeight

        public int getPreferredHeight()
        Description copied from class: Control
        Returns the preferred height of this control.
        Overrides:
        getPreferredHeight in class Control
      • onColorsChanged

        protected void onColorsChanged​(boolean colorsChanged)
        Description copied from class: Control
        Called after a setEnabled, setForeColor and setBackColor and when a control has been added to a Container. If colorsChanged is true, it was called from setForeColor/setBackColor/Container.add; otherwise, it was called from setEnabled
        Overrides:
        onColorsChanged in class Control
      • draw

        protected void draw​(Graphics g)
      • setCursorPos

        public void setCursorPos​(int start,
                                 int end)
        Sets the selected text of this Edit (if start != end). Can be used to set the cursor position, if start equals end. Start must be less or equal to end, and both must be >= 0. It can also be used to clear the selectedText, calling setCursorPos(-1,0). Note: if you're setting the cursor position before the edit is drawn for the first time, the edit will not be scrolled if the end position goes beyond the limits. Important! No bounds checking is made. Be sure to not call this method with invalid positions! Example:
         ed.setText("1234567890123456");
         ed.setCursorPos(3,14);
         ed.requestFocus();
         
      • setCursorPos

        public void setCursorPos​(int pos)
        Sets the cursor position
      • getCursorPos

        public int[] getCursorPos()
        Returns an array with the cursor positions. You can use it with getText to find the selected text String. E.g.:
         int []cursorPos = ed.getCursorPos();
         int start = cursorPos[0];
         int end = cursorPos[1];
         String text = ed.getText();
         if (start != -1) // is the text selected?
         {
            String selectedText = text.substring(start,end);
            ...
         
      • popupKCC

        public void popupKCC()
        User method to popup the keyboard/calendar/calculator for this edit.
      • shiftScreen

        protected void shiftScreen​(boolean force)
      • hideSip

        protected void hideSip()
      • onEvent

        public void onEvent​(Event event)
        Called by the system to pass events to the edit control.
        Overrides:
        onEvent in class Control
        Parameters:
        event - the event to process
        See Also:
        Event, KeyEvent, PenEvent
      • shiftTo

        protected void shiftTo​(Control next)
      • popupsHidden

        public static boolean popupsHidden()
      • onWindowPaintFinished

        protected void onWindowPaintFinished()
        Description copied from class: Control
        Called after the window has finished a paint. Only called to the focused control and the parent's window.
        Overrides:
        onWindowPaintFinished in class Control
      • onPaint

        public void onPaint​(Graphics g)
        Called by the system to draw the edit control.
        Overrides:
        onPaint in class Control
        Parameters:
        g - the graphics object for drawing
        See Also:
        Graphics
      • getLength

        public int getLength()
        Returns the length of the text.
        Since:
        SuperWaba 4.21
      • getTrimmedLength

        public int getTrimmedLength()
        Returns the length of the text after applying a trim to it. This method consumes less memory than getText().trim().length().
        Since:
        TotalCross 1.3
      • clear

        public void clear()
        Clears this control, settings the text to clearValueStr. Note that if the Edit is not editable, you will have to explicitly call the clear method of this Edit.
        Overrides:
        clear in class Control
      • handleGeographicalFocusChangeKeys

        public Control handleGeographicalFocusChangeKeys​(KeyEvent ke)
        Description copied from class: Control
        Used by the main event loop to give the currently focused control an opportunity to act directly on the KeyEvent.
        Overrides:
        handleGeographicalFocusChangeKeys in class Control
        Parameters:
        ke - The KeyEvent to be processed
        Returns:
        The control that should get focus as a result of this KeyEvent. Null if this control did not handle the KeyEvent.
        See Also:
        Settings.geographicalFocus
      • clipboardCopy

        public void clipboardCopy()
        Copies the text to the clipboard. If there's a selected text, copies the portion selected, otherwise, copies the whole text.
        Since:
        TotalCross 1.14
      • clipboardCut

        public void clipboardCut()
        Cuts the selected text to the clipboard.
        Since:
        TotalCross 1.14
      • clipboardPaste

        public void clipboardPaste()
        Paste from the clipboard into the Edit at the current insert position.
        Since:
        TotalCross 1.14
      • getCopy

        public Edit getCopy()
        Returns a copy of this Edit with almost all features. Used by Keyboard and SIPBox classes.
        Since:
        TotalCross 1.27
      • willOpenKeyboard

        protected boolean willOpenKeyboard()
        Description copied from class: Control
        Returns true if this is a MultiEdit or an Edit that has a standard keyboard.
        Overrides:
        willOpenKeyboard in class Control