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

password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

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

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

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

有办法实现吗?


当前回答

基于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)
    }
}

其他回答

已添加密码可见切换功能以支持库版本24.2.0,使您可以直接从EditText切换密码,而不需要复选框。

您可以首先将支持库版本更新到24.2.0,然后在TextInputEditText上设置密码的inputType。下面是如何做到这一点:

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password"
            android:inputType="textPassword"/>
</android.support.design.widget.TextInputLayout>

您可以在TextInputLayout的开发人员文档中获得有关新特性的更多信息。

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

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

edit_pin. setinputtype (InputType.TYPE_CLASS_NUMBER);这样会不会更好的使用两者呢

holder.edit_pin.setInputType(InputType.TYPE_CLASS_NUMBER);
holder.edit_pin.setTransformationMethod(PasswordTransformationMethod.getInstance());

注意:这适用于使用动态控件时,例如使用arrayaapter

这是图像/按钮显示/隐藏密码的完整onClick处理程序。

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

使用变换法:

隐藏:

editText.transformationMethod = PasswordTransformationMethod.getInstance()

可见:

editText.transformationMethod = SingleLineTransformationMethod.getInstance()

就是这样。