Only show number buttons on Soft Keyboard in Android?

前端 未结 8 1409
栀梦
栀梦 2020-12-01 10:06

On the soft keyboard in Android you can set the soft keyboard to show the numbers instead of a-z keyboard using android:inputType=\"numberDecimal\". However, wh

相关标签:
8条回答
  • 2020-12-01 10:49

    Last I looked into it, Android did not have any good options for that. I ended up having to write my own version of a soft keyboard-like user interface.

    0 讨论(0)
  • 2020-12-01 10:50

    The accepted answer did not work for me (tested on OnePlus 3t and Samsung Galaxy S6/S7 phones).

    This solution uses numberPassword but overrides the default transformation method for the EditText to show characters instead of showing dots.

    <EditText
        android:id="@+id/userid"
        android:inputType="numberPassword"
        android:maxLength="6"
    />
    
    // Numeric 6 character user id
    EditText input = findViewById(R.id.userid);
    
    // Process input and show characters instead of dots
    input.setTransformationMethod(SingleLineTransformationMethod.getInstance());
    
    0 讨论(0)
  • 2020-12-01 10:51

    Just posted an answer here but re-posting for simplicity:

    Seems Android has added the functionality we were seeking. This is the xml I use for simple EditText numeric entry:

        android:inputType="numberPassword"
        android:digits="0123456789"
        android:singleLine="true"
        android:ems="4"
        android:textColor="@android:color/black"
        android:gravity="center"
    
    0 讨论(0)
  • 2020-12-01 10:51

    The accepted answer didn't work for me. It kept showing other unwanted characters

    I found a way to accomplish the behavior I wanted by changing the inputType to numberPassword and then disabling the password dots

    textInput.inputType = InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_VARIATION_PASSWORD
    textInput.transformationMethod = SingleLineTransformationMethod()
    
    0 讨论(0)
  • 2020-12-01 10:54

    You must only add this line on your code:

    input.setRawInputType(Configuration.KEYBOARD_12KEY);
    

    this will show only the numeric keyboard.

    0 讨论(0)
  • 2020-12-01 10:58
    android:inputType="phone"
    android:digits="1234567890"
    

    is an option

    0 讨论(0)
提交回复
热议问题