ANDROID - Numeric keyboard

喜欢而已 提交于 2019-12-08 12:43:32

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

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.

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!