How to force EditText to accept only numbers?

前端 未结 6 1341
借酒劲吻你
借酒劲吻你 2020-12-08 18:32

how to force the EditText to accept only numbers.?

6条回答
  •  心在旅途
    2020-12-08 18:56

    This is the final solution:

     public static void enforceEditTextUseNumberOnly(EditText field) {
            Typeface existingTypeface = field.getTypeface();
            field.setInputType((InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD));
            field.setTransformationMethod(new NumericKeyBoardTransformationMethod());
            field.setTypeface(existingTypeface);
            if (field.getParent().getParent() instanceof TextInputLayout) {
                ((TextInputLayout) field.getParent().getParent()).setPasswordVisibilityToggleEnabled(false);
            }
        }
    
      private static class NumericKeyBoardTransformationMethod extends PasswordTransformationMethod {
            @Override
            public CharSequence getTransformation(CharSequence source, View view) {
                return source;
            }
        }
    

提交回复
热议问题