Programmatically change input type of the EditText

2019-01-06 09:55发布

In my application, I have an EditText whose default input type is set to android:inputType="textPassword" by deault. It has a CheckBox to its right, which is when checked, changes the input type of that EditText to NORMAL PLAIN TEXT. Code for that is

password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

My problem is, when that CheckBox is unchecked it should again set the input type to PASSWORD. I've done it using-

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

But, the text inside that edittext is still visible. And for surprise, when I change the orienatation, it automatically sets the input type to PASSWORD and the text inside is bulleted (shown like a password).

Any way to achieve this?

21条回答
Emotional °昔
2楼-- · 2019-01-06 10:20

After you setInputType for a password field, you will have problem with FONT
Here is my solution for show/hide password without font problem

protected void onCreate(Bundle savedInstanceState) {
    ...
    findViewById(R.id.button_show_hide_password).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (isPasswordVisible(edtPassword)) {
                enableInputHiddenPassword(edtPassword);
            } else {
                enableInputVisiblePassword(edtPassword);
            }
            edtPassword.setSelection(edtPassword.getText().length());
        }
    });
}

final int INPUT_TYPE_VISIBLE_PASSWORD = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
final int INPUT_TYPE_HIDDEN_PASSWORD = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;

private boolean isPasswordVisible(EditText editText) {
    return editText.getInputType() == INPUT_TYPE_VISIBLE_PASSWORD;
}

private void enableInputVisiblePassword(EditText editText) {
    Typeface cache = editText.getTypeface();
    editText.setInputType(INPUT_TYPE_VISIBLE_PASSWORD);
    editText.setTypeface(cache);
}

private void enableInputHiddenPassword(EditText editText) {
    Typeface cache = editText.getTypeface();
    editText.setInputType(INPUT_TYPE_HIDDEN_PASSWORD);
    editText.setTypeface(cache);
}

Note: I use InputType.TYPE_TEXT_VARIATION_PASSWORD instead of InputType.TYPE_CLASS_TEXT or HideReturnsTransformationMethod because I want the keyboard display both text and number

DEMO

查看更多
beautiful°
3楼-- · 2019-01-06 10:20

Another simple example using ImageView to toggle visibility with less code, because of single InputType assign we need only equality operator:

EditText inputPassword = (EditText) findViewById(R.id.loginPassword);
ImageView inputPasswordShow = (ImageView) findViewById(R.id.imagePasswordShow);
inputPasswordShow.setOnClickListener(new View.OnClickListener() {
     @Override
     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());
    }
});

Replacing :

InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD

With :

InputType.TYPE_CLASS_TEXT

Will give the same result but shorter word.

查看更多
地球回转人心会变
4楼-- · 2019-01-06 10:23

This is the full onClick handler for the Image/Button to show/hide the password.

    new OnClickListener() {
        @Override
        public void onClick(View v) {
            // current ursor position
            int cursorPosition = edtPassword.getSelectionStart();

            // toggles the control variable
            isPassworsVisible = !isPassworsVisible;

            // sets the image toggler inside edit text
            passwordVisible.setImageDrawable(getResources().getDrawable(isPassworsVisible ? R.drawable.ic_eye_checked : R.drawable.ic_eye_unchecked));

            // apply input type
            edtPassword.setInputType(isPassworsVisible ? InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

            // returns cursor to position
            edtPassword.setSelection(cursorPosition);
        }
    };
查看更多
The star\"
5楼-- · 2019-01-06 10:26
password.setInputType(InputType.TYPE_CLASS_TEXT | inputType.TYPE_TEXT_VARIATION_PASSWORD);

Method above didn't really work for me. Answer below works for 2.2 sdk.

password.setTransformationMethod(PasswordTransformationMethod.getInstance());

Set inputType for an EditText?

查看更多
叛逆
6楼-- · 2019-01-06 10:29

My search for a similar solution for Visual Studio / Xamarin lead me to this thread. Below is what worked for me with Xamarin. Note that this implementation retains the TYPE_TEXT_FLAG_NO_SUGGESTIONS flag when switching between modes.

EditText et = FindViewById<EditText>(Resource.Id.ET);

To show characters: et.InputType = Android.Text.InputTypes.TextVariationVisiblePassword | Android.Text.InputTypes.TextFlagNoSuggestions;

To hide characters: et.InputType = Android.Text.InputTypes.TextVariationPassword | Android.Text.InputTypes.ClassText;

To set position to end: int position = et.Text.Length; et.SetSelection(position, position);

查看更多
仙女界的扛把子
7楼-- · 2019-01-06 10:29

Just an additional comment on the correct answer provided by @Rajkiran, you may want to add

etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

to the NORMAL input state so that the users wont be annoyed by the keyboard's auto-suggestion

查看更多
登录 后发表回答