Stay organized with collections
    
    
      
      Save and categorize content based on your preferences.
    
  
  
    
  
  
  
  
    
    
    
  
  
    
    
  
  
NumberKeyListener
  public
  
  
  abstract
  class
  NumberKeyListener
  
  
  
  
  
  
  
    extends BaseKeyListener
  
  
  
  
  
      implements
      
        InputFilter
      
  
  
  
  
  
    
  For numeric text entry
 
 As for all implementations of 
KeyListener, this class is only concerned
 with hardware keyboards.  Software input methods have no obligation to trigger
 the methods in this class.
Summary
Public methods | 
  
  
  
    
        
        
        
        
        
        CharSequence
     | 
  
    
      
      filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
      
      
        This method is called when the buffer is going to replace the
 range dstart … dend of dest
 with the new text from the range start … end
 of source.
        
    
 
      
     | 
  
  
  
  
  
    
        
        
        
        
        
        boolean
     | 
  
    
      
      onKeyDown(View view, Editable content, int keyCode, KeyEvent event)
      
      
        Handles presses of the meta keys.
        
    
 
      
     | 
  
  
Inherited methods | 
  
    From class
      
        
          android.text.method.BaseKeyListener
        
      
    
    
      
  
  
  
    
        
        
        
        
        
        boolean
     | 
  
    
      
      backspace(View view, Editable content, int keyCode, KeyEvent event)
      
      
        Performs the action that happens when you press the KeyEvent.KEYCODE_DEL key in
 a TextView.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        boolean
     | 
  
    
      
      forwardDelete(View view, Editable content, int keyCode, KeyEvent event)
      
      
        Performs the action that happens when you press the KeyEvent.KEYCODE_FORWARD_DEL
 key in a TextView.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        boolean
     | 
  
    
      
      onKeyDown(View view, Editable content, int keyCode, KeyEvent event)
      
      
        Handles presses of the meta keys.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        boolean
     | 
  
    
      
      onKeyOther(View view, Editable content, KeyEvent event)
      
      
        Base implementation handles ACTION_MULTIPLE KEYCODE_UNKNOWN by inserting
 the event's text into the content.
        
    
 
      
     | 
   
  
     
   
 | 
  
    From class
      
        
          android.text.method.MetaKeyKeyListener
        
      
    
    
      
  
  
  
    
        
        
        static
        
        
        void
     | 
  
    
      
      adjustMetaAfterKeypress(Spannable content)
      
      
        Call this method after you handle a keypress so that the meta
 state will be reset to unshifted (if it is not still down)
 or primed to be reset to unshifted (once it is released).
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        
        
        long
     | 
  
    
      
      adjustMetaAfterKeypress(long state)
      
      
        Call this method after you handle a keypress so that the meta
 state will be reset to unshifted (if it is not still down)
 or primed to be reset to unshifted (once it is released).
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        
        
        void
     | 
  
    
      
      clearMetaKeyState(Editable content, int states)
      
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        long
     | 
  
    
      
      clearMetaKeyState(long state, int which)
      
      
        Clears the state of the specified meta key if it is locked.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        void
     | 
  
    
      
      clearMetaKeyState(View view, Editable content, int states)
      
      
     | 
   
  
  
  
  
    
        
        
        static
        final
        
        int
     | 
  
    
      
      getMetaState(CharSequence text)
      
      
        Gets the state of the meta keys.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        final
        
        int
     | 
  
    
      
      getMetaState(CharSequence text, int meta)
      
      
        Gets the state of a particular meta key.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        final
        
        int
     | 
  
    
      
      getMetaState(long state)
      
      
        Gets the state of the meta keys.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        final
        
        int
     | 
  
    
      
      getMetaState(CharSequence text, KeyEvent event)
      
      
        Gets the state of the meta keys for a specific key event.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        final
        
        int
     | 
  
    
      
      getMetaState(long state, int meta)
      
      
        Gets the state of a particular meta key.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        final
        
        int
     | 
  
    
      
      getMetaState(CharSequence text, int meta, KeyEvent event)
      
      
        Gets the state of a particular meta key to use with a particular key event.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        
        
        long
     | 
  
    
      
      handleKeyDown(long state, int keyCode, KeyEvent event)
      
      
        Handles presses of the meta keys.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        
        
        long
     | 
  
    
      
      handleKeyUp(long state, int keyCode, KeyEvent event)
      
      
        Handles release of the meta keys.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        
        
        boolean
     | 
  
    
      
      isMetaTracker(CharSequence text, Object what)
      
      
        Returns true if this object is one that this class would use to
 keep track of any meta state in the specified text.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        
        
        boolean
     | 
  
    
      
      isSelectingMetaTracker(CharSequence text, Object what)
      
      
        Returns true if this object is one that this class would use to
 keep track of the selecting meta state in the specified text.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        boolean
     | 
  
    
      
      onKeyDown(View view, Editable content, int keyCode, KeyEvent event)
      
      
        Handles presses of the meta keys.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        boolean
     | 
  
    
      
      onKeyUp(View view, Editable content, int keyCode, KeyEvent event)
      
      
        Handles release of the meta keys.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        
        
        long
     | 
  
    
      
      resetLockedMeta(long state)
      
      
        Call this if you are a method that ignores the locked meta state
 (arrow keys, for example) and you handle a key.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        
        
        void
     | 
  
    
      
      resetLockedMeta(Spannable content)
      
      
        Call this if you are a method that ignores the locked meta state
 (arrow keys, for example) and you handle a key.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        static
        
        
        void
     | 
  
    
      
      resetMetaState(Spannable text)
      
      
        Resets all meta state to inactive.
        
    
 
      
     | 
   
  
     
   
 | 
  
    From class
      
        
          java.lang.Object
        
      
    
    
      
  
  
  
    
        
        
        
        
        
        Object
     | 
  
    
      
      clone()
      
      
        Creates and returns a copy of this object.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        boolean
     | 
  
    
      
      equals(Object obj)
      
      
        Indicates whether some other object is "equal to" this one.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        void
     | 
  
    
      
      finalize()
      
      
        Called by the garbage collector on an object when garbage collection
 determines that there are no more references to the object.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        final
        
        Class<?>
     | 
  
    
      
      getClass()
      
      
        Returns the runtime class of this Object.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        int
     | 
  
    
      
      hashCode()
      
      
        Returns a hash code value for the object.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        final
        
        void
     | 
  
    
      
      notify()
      
      
        Wakes up a single thread that is waiting on this object's
 monitor.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        final
        
        void
     | 
  
    
      
      notifyAll()
      
      
        Wakes up all threads that are waiting on this object's monitor.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        
        
        String
     | 
  
    
      
      toString()
      
      
        Returns a string representation of the object.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        final
        
        void
     | 
  
    
      
      wait(long timeoutMillis, int nanos)
      
      
        Causes the current thread to wait until it is awakened, typically
 by being notified or interrupted, or until a
 certain amount of real time has elapsed.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        final
        
        void
     | 
  
    
      
      wait(long timeoutMillis)
      
      
        Causes the current thread to wait until it is awakened, typically
 by being notified or interrupted, or until a
 certain amount of real time has elapsed.
        
    
 
      
     | 
   
  
  
  
  
    
        
        
        
        final
        
        void
     | 
  
    
      
      wait()
      
      
        Causes the current thread to wait until it is awakened, typically
 by being notified or interrupted.
        
    
 
      
     | 
   
  
     
   
 | 
  
    From interface
      
        
          android.text.method.KeyListener
        
      
    
    
      
  
  
  
    
        abstract
        
        
        
        
        void
     | 
  
    
      
      clearMetaKeyState(View view, Editable content, int states)
      
      
        Remove the given shift states from the edited text.
        
    
 
      
     | 
   
  
  
  
  
    
        abstract
        
        
        
        
        int
     | 
  
    
      
      getInputType()
      
      
        Return the type of text that this key listener is manipulating,
 as per InputType.
        
    
 
      
     | 
   
  
  
  
  
    
        abstract
        
        
        
        
        boolean
     | 
  
    
      
      onKeyDown(View view, Editable text, int keyCode, KeyEvent event)
      
      
        If the key listener wants to handle this key, return true,
 otherwise return false and the caller (i.e. the widget host)
 will handle the key.
        
    
 
      
     | 
   
  
  
  
  
    
        abstract
        
        
        
        
        boolean
     | 
  
    
      
      onKeyOther(View view, Editable text, KeyEvent event)
      
      
        If the key listener wants to other kinds of key events, return true,
 otherwise return false and the caller (i.e. the widget host)
 will handle the key.
        
    
 
      
     | 
   
  
  
  
  
    
        abstract
        
        
        
        
        boolean
     | 
  
    
      
      onKeyUp(View view, Editable text, int keyCode, KeyEvent event)
      
      
        If the key listener wants to handle this key release, return true,
 otherwise return false and the caller (i.e. the widget host)
 will handle the key.
        
    
 
      
     | 
   
  
     
   
 | 
| 
  
 | 
Public constructors
    NumberKeyListener
    
public NumberKeyListener ()
    
    
    
  
 
Public methods
    filter
    
public CharSequence filter (CharSequence source, 
                int start, 
                int end, 
                Spanned dest, 
                int dstart, 
                int dend)
    
    
    
  This method is called when the buffer is going to replace the
 range dstart … dend of dest
 with the new text from the range start … end
 of source.  Return the CharSequence that you would
 like to have placed there instead, including an empty string
 if appropriate, or null to accept the original
 replacement.  Be careful to not to reject 0-length replacements,
 as this is what happens when you delete text.  Also beware that
 you should not attempt to make any changes to dest
 from this method; you may only examine it for context.
 Note: If source is an instance of Spanned or
 Spannable, the span objects in the source should be
 copied into the filtered result (i.e. the non-null return value).
 TextUtils.copySpansFrom can be used for convenience if the
 span boundary indices would be remaining identical relative to the source.
    
    | Parameters | 
      
        source | 
        
          CharSequence  | 
      
      
        start | 
        
          int  | 
      
      
        end | 
        
          int  | 
      
      
        dest | 
        
          Spanned  | 
      
      
        dstart | 
        
          int  | 
      
      
        dend | 
        
          int  | 
      
    
    
 
    onKeyDown
    
public boolean onKeyDown (View view, 
                Editable content, 
                int keyCode, 
                KeyEvent event)
    
    
    
  Handles presses of the meta keys.
    
    | Parameters | 
      
        view | 
        
          View  | 
      
      
        content | 
        
          Editable  | 
      
      
        keyCode | 
        
          int  | 
      
      
        event | 
        
          KeyEvent  | 
      
    
    
 
Protected methods
    getAcceptedChars
    
protected abstract char[] getAcceptedChars ()
    
    
    
  You can say which characters you can accept.
    
      | Returns | 
      
        char[] | 
        This value cannot be null. | 
      
    
 
    lookup
    
protected int lookup (KeyEvent event, 
                Spannable content)
    
    
    
  
    
    | Parameters | 
      
        event | 
        
          KeyEvent  | 
      
      
        content | 
        
          Spannable  | 
      
    
    
 
    ok
    
protected static boolean ok (char[] accept, 
                char c)
    
    
    
  
    
    | Parameters | 
      
        accept | 
        
          char  | 
      
      
        c | 
        
          char  | 
      
    
    
 
 
 
  
  
    
  
 
  
    
      
      
    
    
      
    
    
  
       
    
    
      
    
  
  
  Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
  Last updated 2025-02-10 UTC.
  
  
  
    
      [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[]]