如何使用setTransformationMethod(null)显示密码,但保留眼睛图标

时间:2017-04-24 18:15:54

标签: android

目前,我将密码输入文本字段设计如下

<android.support.design.widget.TextInputLayout
    android:layout_marginLeft="@dimen/trading_activity_horizontal_margin"
    android:layout_marginRight="@dimen/trading_activity_horizontal_margin"

    app:hintTextAppearance="@style/TradingWizardTextInputLayout"
    app:passwordToggleEnabled="true"
    android:id="@+id/password_text_input_layout"
    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/wizard_password"
        android:id="@+id/password_edit_text"

        android:inputType="textPassword"
        android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>

看起来如下

enter image description here

但是,在初始阶段,我希望我的密码输入文本字段的输入可见。

这意味着,当首次出现密码输入文本字段时,它应该如下所示

enter image description here

我曾尝试过

passwordEditText.setTransformationMethod(null)

密码可见。但是,它也会使眼睛图标消失!

enter image description here

如何使用setTransformationMethod(null)显示密码,同时保留眼睛图标?

1 个答案:

答案 0 :(得分:0)

而不是使用

app:passwordToggleEnabled="true"

你可以设置一个&#34; drawableRight&#34;和一个touchListener来获得相同的效果。 代码将是这样的

XML

<android.support.design.widget.TextInputLayout
                    android:id="@+id/password_text_input"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:hintEnabled="false"
                    android:layout_marginLeft="@dimen/dimen_18"
                    android:layout_marginRight="@dimen/dimen_18">
                    <EditText
                        android:textSize="16sp"
                        android:paddingTop="0dp"
                        android:id="@+id/password_edit_text"
                        android:textColor="@android:color/black"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:drawableRight="@drawable/ic_hide_password"
                        android:maxLines="1"
                        />
                </android.support.design.widget.TextInputLayout>

<强> TouchListener

public class ShowHidePassword implements View.OnTouchListener{
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final int DRAWABLE_RIGHT = 2;
        EditText editView = (EditText) v;
        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (event.getRawX() >= (editView.getRight() - editView.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                editView.requestFocus();
                if(editView.getTransformationMethod() instanceof PasswordTransformationMethod){
                    editView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    editView.setSelection(editView.getText().length());
                    editView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_hide_password, 0);
                }else{
                    editView.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    editView.setSelection(editView.getText().length());
                    editView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_view_password, 0);
                }
                return true;
            }
        }

        return false;
    }
}

并在您的活动中设置监听器

passwordEditText.setOnTouchListener(new ShowHidePassword());

希望这会有所帮助。