4

Activity.xml

    <EditText
        android:id="@+id/et_pwd"
        android:layout_width="300dp"
        android:layout_height="37dp"
        android:layout_marginBottom="15dp"
        android:drawableLeft="@drawable/icon_password"
        android:hint="Password"
        android:inputType="textPassword"
        android:maxLength="20"
        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" />

Button OnClickListner

if (et_password.getInputType() != (InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD)) {
   et_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
} else {
   et_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}

First of all, I set digits on the EditText, and if I click a button it implements the above code which changes its inputType. Before I change the inputType, its digits are working. However, after changing its inputType its digits are not working for some reasons. It allows special characters to be entered. How can I improve my code to make it work?

MyNameIsAsker
  • 101
  • 10

1 Answers1

0

This Code Change Plain text to Password Text And Vise-Versa.

public void onClick(View view) {
         if(inputPassword.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {
              inputPassword.setInputType( InputType.TYPE_CLASS_TEXT |
                                        InputType.TYPE_TEXT_VARIATION_PASSWORD);
         }else {
              inputPassword.setInputType( InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD );
         }
         inputPassword.setSelection(inputPassword.getText().length());
    }

And use this filter code for digits.

InputFilter filter = new InputFilter() {
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {
        for (int i = start; i < end; i++) {
            if (!Character.isLetterOrDigit(source.charAt(i))) {
                return "";
            }
        }
        return null;
    }
};
edit.setFilters(new InputFilter[] { filter });
anonymous
  • 401
  • 7
  • 15