Sebosin
Sebosin

Reputation: 177

ANDROID - Numeric keyboard

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.

Upvotes: 1

Views: 6988

Answers (4)

Mahesh
Mahesh

Reputation: 2892

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

Upvotes: 1

Wajdi Hh
Wajdi Hh

Reputation: 785

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.

Upvotes: 0

Mark D
Mark D

Reputation: 3327

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.

Upvotes: 1

Simon
Simon

Reputation: 14472

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

Upvotes: 6

Related Questions