How do I show the number keyboard on an EditText i

2019-01-08 07:47发布

问题:

I just basically want to switch to the number pad mode as soon a certain EditText has the focus.

回答1:

You can configure an inputType for your EditText:

<EditText android:inputType="number" ... />


回答2:

To do it in a Java file:

EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);


回答3:

I found this implementation useful, shows a better keyboard and limits input chars.

<EditText
    android:inputType="phone"
    android:digits="1234567890"
    ...
/>

Additionally, you could use android:maxLength to limit the max amount of numbers.

To do this programmatically:

editText.setInputType(InputType.TYPE_CLASS_PHONE);

KeyListener keyListener = DigitsKeyListener.getInstance("1234567890");
editText.setKeyListener(keyListener);


回答4:

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ...
    android:inputType="number|phone"/> 

will show the large number pad as dialer.



回答5:

You can do it 2 ways

  1. Runtime set

    EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    
  2. Using XML

    <EditText
        ...
        android:inputType="number" />
    


回答6:

If you need fractional number, then this is the answer for you:

android:inputType="numberDecimal"


回答7:

If you are using your EditText in Dialog or creating dynamically and You can't set it from xml then this example will help you in setting that type of key board while you are using dynamic Edit Text etc

myEditTxt.setInputType(InputType.TYPE_CLASS_NUMBER);

where myEditTxt is the dynamic EDIT TEXT object(name)



回答8:

More input types and details found here on google website



回答9:

editText.setRawInputType(InputType.TYPE_CLASS_NUMBER);


回答10:

Use the below code in java file

editText.setRawInputType(Configuration.KEYBOARD_QWERTY);



回答11:

Define this in your xml code

android:inputType="number"


回答12:

EditText number1 = (EditText) layout.findViewById(R.id.edittext); 
number1.setInputType(InputType.TYPE_CLASS_NUMBER);


回答13:

EditText number1 = (EditText) layout.findViewById(R.id.edittext); 
    number1.setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_CLASS_PHONE);
     number1.setKeyListener(DigitsKeyListener.getInstance("0123456789”));

This code will only takes numbers from "0123456789”, even if you accidentally type other than "0123456789”, edit text will not accept.