1

Good afternoon.

I have an EditText that may contain numbers and letters. By deploying the keyboard, is there any way to deploy it in numerical form?.

If I put the EditText as numeric I do, but that's not my case.

Thank you very much.

Sebosin
  • 167
  • 6
  • 16
  • 1
    Are you doing this in code? Do you mean that you haven't tagged the EditText as numeric but you want to show numeric keyboard under some circumstances? – Simon Mar 28 '12 at 17:23

4 Answers4

6

Try this:

EditText myEditText = (EditText)findViewById(R.id.myEditText);
myEditText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);

Then set it back to alphanumeric when you are done with numeric..

Simon
  • 14,407
  • 8
  • 46
  • 61
  • 1
    Needs to include InputType.TYPE_CLASS_NUMBER like this: myEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); – pstoppani Jan 18 '13 at 00:39
1

Using XML:

   <EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edittext"
        android:inputType="number"/>

or

Using code:

EditText editText=new EditText(this);
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
Mahesh
  • 2,862
  • 2
  • 31
  • 41
1

You can:

  • Hide/Show different EditTexts that have different input types
  • Dynamically set the input type of your EditText as needed (editText.setInputType(InputType.TYPE_WHATEVER)

You cannot:

  • Default an IME on an EditText with inputType text to the "Numeric" layout.
Mark D
  • 3,317
  • 1
  • 26
  • 27
0

In your oncreate method you can put this bloc of code :

final EditText myEditJava = (EditText) findViewById(R.id.myEdit);

    myEditJava.addTextChangedListener(new TextWatcher() {

        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        public void afterTextChanged(Editable s) {}

        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
                if (s.toString().matches("((-|\\+)?[0-9]+(\\.[0-9]+)?)+"))
                    myEditJava.setInputType(InputType.TYPE_CLASS_PHONE);
                else
                    myEditJava.setInputType(InputType.TYPE_CLASS_TEXT);

        }
    });

So when you put the first char if It will be a Numeric Value the keyborad will be displayed on the Numeric input mode , else on text input mode.

Wajdi Hh
  • 785
  • 3
  • 9