open class DigitsKeyListener : NumberKeyListener
kotlin.Any
   ↳ android.text.method.MetaKeyKeyListener
   ↳ android.text.method.BaseKeyListener
   ↳ android.text.method.NumberKeyListener
   ↳ android.text.method.DigitsKeyListener

For digits-only 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

Inherited constants

From class MetaKeyKeyListener

Int META_ALT_LOCKED

Flag that indicates that the ALT key is locked.

Int META_ALT_ON

Flag that indicates that the ALT key is on. Value equals KeyEvent.META_ALT_ON.

Int META_CAP_LOCKED

Flag that indicates that the SHIFT key is locked in CAPS mode.

Int META_SHIFT_ON

Flag that indicates that the SHIFT key is on. Value equals KeyEvent.META_SHIFT_ON.

Int META_SYM_LOCKED

Flag that indicates that the SYM key is locked.

Int META_SYM_ON

Flag that indicates that the SYM key is on. Value equals KeyEvent.META_SYM_ON.

Public constructors

Allocates a DigitsKeyListener that accepts the ASCII digits 0 through 9.

DigitsKeyListener(sign: Boolean, decimal: Boolean)

Allocates a DigitsKeyListener that accepts the ASCII digits 0 through 9, plus the ASCII plus or minus sign (only at the beginning) and/or the ASCII period ('.') as the decimal point (only one per field) if specified.

DigitsKeyListener(locale: Locale?, sign: Boolean, decimal: Boolean)

Public methods
open CharSequence!

filter(source: CharSequence!, start: Int, end: Int, dest: Spanned!, dstart: Int, dend: Int)

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.

open Int

Returns the input type for the listener.

open static DigitsKeyListener

Returns a DigitsKeyListener that accepts the ASCII digits 0 through 9.

open static DigitsKeyListener

getInstance(sign: Boolean, decimal: Boolean)

Returns a DigitsKeyListener that accepts the ASCII digits 0 through 9, plus the ASCII plus or minus sign (only at the beginning) and/or the ASCII period ('.') as the decimal point (only one per field) if specified.

open static DigitsKeyListener

getInstance(accepted: String)

Returns a DigitsKeyListener that accepts only the characters that appear in the specified String.

open static DigitsKeyListener

getInstance(locale: Locale?)

Returns a DigitsKeyListener that accepts the locale-appropriate digits.

open static DigitsKeyListener

getInstance(locale: Locale?, sign: Boolean, decimal: Boolean)

Returns a DigitsKeyListener that accepts the locale-appropriate digits, plus the locale-appropriate plus or minus sign (only at the beginning) and/or the locale-appropriate decimal separator (only one per field) if specified.

Protected methods
open CharArray

You can say which characters you can accept.

Inherited functions

From class MetaKeyKeyListener

Unit adjustMetaAfterKeypress(content: Spannable!)

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).

Long adjustMetaAfterKeypress(state: Long)

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). Takes the current state, returns the new state.

Unit clearMetaKeyState(view: View!, content: Editable!, states: Int)
Long clearMetaKeyState(state: Long, which: Int)

Clears the state of the specified meta key if it is locked.

Unit clearMetaKeyState(content: Editable!, states: Int)
Int getMetaState(text: CharSequence!)

Gets the state of the meta keys.

Int getMetaState(text: CharSequence!, event: KeyEvent!)

Gets the state of the meta keys for a specific key event. For input devices that use toggled key modifiers, the `toggled' state is stored into the text buffer. This method retrieves the meta state for this event, accounting for the stored state. If the event has been created by a device that does not support toggled key modifiers, like a virtual device for example, the stored state is ignored.

Int getMetaState(text: CharSequence!, meta: Int)

Gets the state of a particular meta key.

Int getMetaState(text: CharSequence!, meta: Int, event: KeyEvent!)

Gets the state of a particular meta key to use with a particular key event. If the key event has been created by a device that does not support toggled key modifiers, like a virtual keyboard for example, only the meta state in the key event is considered.

Int getMetaState(state: Long)

Gets the state of the meta keys.

Int getMetaState(state: Long, meta: Int)

Gets the state of a particular meta key.

Long handleKeyDown(state: Long, keyCode: Int, event: KeyEvent!)

Handles presses of the meta keys.

Long handleKeyUp(state: Long, keyCode: Int, event: KeyEvent!)

Handles release of the meta keys.

Boolean isMetaTracker(text: CharSequence!, what: Any!)

Returns true if this object is one that this class would use to keep track of any meta state in the specified text.

Boolean isSelectingMetaTracker(text: CharSequence!, what: Any!)

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 onKeyUp(view: View!, content: Editable!, keyCode: Int, event: KeyEvent!)

Handles release of the meta keys.

Unit resetLockedMeta(content: Spannable!)

Call this if you are a method that ignores the locked meta state (arrow keys, for example) and you handle a key.

Long resetLockedMeta(state: Long)

Call this if you are a method that ignores the locked meta state (arrow keys, for example) and you handle a key.

Unit resetMetaState(text: Spannable!)

Resets all meta state to inactive.

From class BaseKeyListener

Boolean backspace(view: View!, content: Editable!, keyCode: Int, event: KeyEvent!)

Performs the action that happens when you press the KeyEvent.KEYCODE_DEL key in a TextView. If there is a selection, deletes the selection; otherwise, deletes the character before the cursor, if any; ALT+DEL deletes everything on the line the cursor is on.

Boolean forwardDelete(view: View!, content: Editable!, keyCode: Int, event: KeyEvent!)

Performs the action that happens when you press the KeyEvent.KEYCODE_FORWARD_DEL key in a TextView. If there is a selection, deletes the selection; otherwise, deletes the character before the cursor, if any; ALT+FORWARD_DEL deletes everything on the line the cursor is on.

Boolean onKeyOther(view: View!, content: Editable!, event: KeyEvent!)

Base implementation handles ACTION_MULTIPLE KEYCODE_UNKNOWN by inserting the event's text into the content.

From class KeyListener

Int getInputType()

Return the type of text that this key listener is manipulating, as per android.text.InputType. This is used to determine the mode of the soft keyboard that is shown for the editor.

If you return android.text.InputType#TYPE_NULL then no soft keyboard will provided. In other words, you must be providing your own key pad for on-screen input and the key listener will be used to handle input from a hard keyboard.

If you return any other value, a soft input method will be created when the user puts focus in the editor, which will provide a keypad and also consume hard key events. This means that the key listener will generally not be used, instead the soft input method will take care of managing key input as per the content type returned here.

From class NumberKeyListener

Int lookup(event: KeyEvent!, content: Spannable!)
Boolean ok(accept: CharArray!, c: Char)
Boolean onKeyDown(view: View!, content: Editable!, keyCode: Int, event: KeyEvent!)

Handles presses of the meta keys.

Public constructors

DigitsKeyListener

DigitsKeyListener()

Deprecated: Use DigitsKeyListener(Locale) instead.

Allocates a DigitsKeyListener that accepts the ASCII digits 0 through 9.

DigitsKeyListener

DigitsKeyListener(
    sign: Boolean,
    decimal: Boolean)

Deprecated: Use DigitsKeyListener(Locale,boolean,boolean) instead.

Allocates a DigitsKeyListener that accepts the ASCII digits 0 through 9, plus the ASCII plus or minus sign (only at the beginning) and/or the ASCII period ('.') as the decimal point (only one per field) if specified.

DigitsKeyListener

DigitsKeyListener(locale: Locale?)
Parameters
locale Locale?: This value may be null.

DigitsKeyListener

DigitsKeyListener(
    locale: Locale?,
    sign: Boolean,
    decimal: Boolean)
Parameters
locale Locale?: This value may be null.

Public methods

filter

open fun filter(
    source: CharSequence!,
    start: Int,
    end: Int,
    dest: Spanned!,
    dstart: Int,
    dend: Int
): CharSequence!

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.

getInputType

open fun getInputType(): Int

Returns the input type for the listener.

getInstance

open static fun getInstance(): DigitsKeyListener

Deprecated: Use getInstance(Locale) instead.

Returns a DigitsKeyListener that accepts the ASCII digits 0 through 9.

Return
DigitsKeyListener This value cannot be null.

getInstance

open static fun getInstance(
    sign: Boolean,
    decimal: Boolean
): DigitsKeyListener

Deprecated: Use getInstance(Locale,boolean,boolean) instead.

Returns a DigitsKeyListener that accepts the ASCII digits 0 through 9, plus the ASCII plus or minus sign (only at the beginning) and/or the ASCII period ('.') as the decimal point (only one per field) if specified.

Return
DigitsKeyListener This value cannot be null.

getInstance

open static fun getInstance(accepted: String): DigitsKeyListener

Returns a DigitsKeyListener that accepts only the characters that appear in the specified String. Note that not all characters may be available on every keyboard.

Parameters
accepted String: This value cannot be null.
Return
DigitsKeyListener This value cannot be null.

getInstance

open static fun getInstance(locale: Locale?): DigitsKeyListener

Returns a DigitsKeyListener that accepts the locale-appropriate digits.

Parameters
locale Locale?: This value may be null.
Return
DigitsKeyListener This value cannot be null.

getInstance

open static fun getInstance(
    locale: Locale?,
    sign: Boolean,
    decimal: Boolean
): DigitsKeyListener

Returns a DigitsKeyListener that accepts the locale-appropriate digits, plus the locale-appropriate plus or minus sign (only at the beginning) and/or the locale-appropriate decimal separator (only one per field) if specified.

Parameters
locale Locale?: This value may be null.
Return
DigitsKeyListener This value cannot be null.

Protected methods

getAcceptedChars

protected open fun getAcceptedChars(): CharArray

You can say which characters you can accept.

Return
CharArray This value cannot be null.

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 2026-02-26 UTC.