我可以在没有 TextInputEditText 的情况下进行密码切换吗?

Can I get password toggle without TextInputEditText?

我只是想让眼睛按钮让用户切换密码(这样他们就可以验证他们输入的内容)。我在 Whosebug 中找到了解决方案;它正在使用 android.support.design.widget.TextInputEditText。问题是它明显比正常 EditText 厚。

我的 EditText 不在文本输入布局中,因为我不想要提示动画等。我不想硬编码它们的高度,因为它可能因系统语言和主题而异。

我可以使用普通的 EditText 获得切换功能吗?或者我可以让 TextInputEditText 与 EditText 具有相同的高度吗?

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="121dp"
        android:background="#66FF0000"
        android:text="This is normal EditText"
        android:inputType="textEmailAddress"/>

    <android.support.design.widget.TextInputLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="156dp"
        app:hintEnabled="false"
        app:passwordToggleEnabled="true">

        <android.support.design.widget.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#660000FF"
            android:hint="This is TextInputEditText"
            android:inputType="textPassword"/>
    </android.support.design.widget.TextInputLayout>

</RelativeLayout>

似乎是 TextInputLayout 造成了这个问题。我已经问了另一个问题。 Preventing TextInputLayout from making TextInputEditText taller

使用此代码

 public void ShowPasswordCheckBoxListener(View view) {
    boolean checked = ((CheckBox) view).isChecked();
    switch (view.getId()) {
        case R.id.button1:
            if (!checked)
                password1.setTransformationMethod(new PasswordTransformationMethod());  //hide the password from the edit text
            else
               password1.setTransformationMethod(new DoNothingTransformation()); //show the password from the edit text
            break;
        case R.id.button2:
            if (!checked)
                password2.setTransformationMethod(new PasswordTransformationMethod()); //hide the password from the edit text
            else
                password2.setTransformationMethod(null); // another option show the password from the edit text
            break;
    }
}     //password1 and password2 are your normal edittext

你可以使用这个库

compile 'com.github.scottyab:showhidepasswordedittext:0.8'

查看 this link 了解更多信息。