在我的应用程序中,我有一个EditText,其默认输入类型设置为android:inputType="textPassword"默认。它的右侧有一个复选框,选中该复选框时,将该编辑文本的输入类型更改为普通纯文本。它的代码是

password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

我的问题是,当复选框未选中时,它应该再次设置输入类型为密码。我用过-

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

但是,编辑文本中的文本仍然可见。令人惊讶的是,当我改变方向时,它会自动将输入类型设置为PASSWORD,其中的文本是项目符号(显示为密码)。

有办法实现吗?


当前回答

当你为密码字段设置tinputtype后,你将会遇到字体问题 这是我的解决方案显示/隐藏密码没有字体问题

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

注意:我使用InputType。TYPE_TEXT_VARIATION_PASSWORD代替InputType。TYPE_CLASS_TEXT或HideReturnsTransformationMethod,因为我想让键盘同时显示文本和数字

DEMO

其他回答

这招对我很管用:

mytext.setInputType(InputType.TYPE_CLASS_NUMBER);

使用此代码将密码更改为文本,反之亦然

mCbShowPwd.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // checkbox status is changed from uncheck to checked.
                if (!isChecked) {
                        // hide password
                    mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                        // show password
                    mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
            }
        });

完整的示例代码请参考http://www.codeproject.com/Tips/518641/Show-hide-password-in-a-edit-text-view-password-ty

基于neeraj t和Everton Fernandes Rosario的回答,我写在Kotlin,其中密码是你的布局中的EditText的id。

// Show passwords' symbols.
private fun showPassword() {
    password.run {
        val cursorPosition = selectionStart
        transformationMethod = HideReturnsTransformationMethod.getInstance()
        setSelection(cursorPosition)
    }
}

// Show asterisks.
private fun hidePassword() {
    password.run {
        val cursorPosition = selectionStart
        transformationMethod = PasswordTransformationMethod.getInstance()
        setSelection(cursorPosition)
    }
}

好吧,经过几个小时的尝试,终于实现了。下面是代码..

  buttons.get(2).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View view) {
       if(buttons.get(2).getText().toString().equalsIgnoreCase(getResources().getString(R.string.show))){
           editTexts.get(1).setInputType(InputType.TYPE_CLASS_TEXT);
           editTexts.get(1).setSelection(editTexts.get(1).getText().length());
           buttons.get(2).setText(getResources().getString(R.string.hide));
        }else{
           editTexts.get(1).setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
           //editTexts.get(1).setTransformationMethod(PasswordTransformationMethod.getInstance());
           editTexts.get(1).setSelection(editTexts.get(1).getText().length());
           buttons.get(2).setText(getResources().getString(R.string.show));
       }

    }
});

解释:-我有一个按钮与默认文本显示。 onclick事件后,它检查按钮的文本是否显示。 如果它是显示,然后改变输入类型,调整光标的位置,并设置新的文本隐藏在其中。

当它藏起来的时候……做反向,即隐藏密码,调整光标和设置文本显示。就是这样。它像魔法一样有效。

password.setInputType(InputType.TYPE_CLASS_TEXT | inputType.TYPE_TEXT_VARIATION_PASSWORD);

上面的方法并不适合我。下面的答案适用于2.2 sdk。

password.setTransformationMethod (PasswordTransformationMethod.getInstance ());

为EditText设置inputType ?