有没有一个聪明的方法让用户在隐藏和查看密码之间切换在一个android EditText? 许多基于PC的应用程序都允许用户这样做。


当前回答

我所做的是

创建一个编辑文本视图和一个普通文本视图 通过使用约束布局使它们相互重叠(就像Facebook应用登录屏幕一样) 将onClickListener附加到普通文本视图,以便它相应地改变编辑文本视图的输入类型(可见/不可见)

你可以看看这个视频,了解更详细的步骤和解释https://youtu.be/md3eVaRzdIM

希望能有所帮助。

其他回答

你可以使用app:passwordToggleEnabled="true"

下面是一个例子

<android.support.design.widget.TextInputLayout
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true"
        android:textColorHint="@color/colorhint"
        android:textColor="@color/colortext">

从支持库v24.2.0开始,这真的很容易实现。

你需要做的就是:

Add the design library to your dependencies dependencies { compile "com.android.support:design:24.2.0" } Use TextInputEditText in conjunction with TextInputLayout <android.support.design.widget.TextInputLayout android:id="@+id/etPasswordLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:passwordToggleEnabled="true" android:layout_marginBottom="@dimen/login_spacing_bottom"> <android.support.design.widget.TextInputEditText android:id="@+id/etPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/fragment_login_password_hint" android:inputType="textPassword"/> </android.support.design.widget.TextInputLayout>

passwordToggleEnabled属性将完成这项工作!

在你的根布局中,不要忘记添加xmlns:app="http://schemas.android.com/apk/res-auto" 您可以使用以下方法自定义密码切换:

app:passwordToggleDrawable -可绘制的使用作为密码输入可见切换图标。 app:passwordToggleTint -用于密码输入可见切换的图标。 app:passwordToggleTintMode -用于应用背景色调的混合模式。

更多细节在TextInputLayout文档。

对于安卓X

将android. design.widget. textinputlayout替换为com.google.android.material.textfield.TextInputLayout 将android.support.design.widget.TextInputEditText替换为com.google.android.material.textfield.TextInputEditText

如果你想要一个简单的解决方案,你可以使用这个扩展的Android的EditText去这个链接了解更多信息:https://github.com/scottyab/showhidepasswordedittext

将此添加到build.gradle中: 实现“com.github.scottyab: showhidepasswordedittext: 0.8”

然后更改XML文件中的EditText。

来自:< EditText

: < com.scottyab.showhidepasswordedittext.ShowHidePasswordEditText

这是所有。

注意:在XML设计中无法看到它,请尝试在模拟器或物理设备中运行它。

在XML中这样做

    <LinearLayout
          android:layout_height="wrap_content"
          android:layout_width="fill_parent"
          android:orientation="vertical"
          >
          <RelativeLayout
              android:id="@+id/REFReLayTellFriend"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              >
          <EditText
              android:id="@+id/etpass1"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:background="@android:color/transparent"
              android:bottomLeftRadius="10dp"
              android:bottomRightRadius="50dp"
              android:fontFamily="@font/frutiger"
              android:gravity="start"
              android:inputType="textPassword"
              android:hint="@string/regpass_pass1"
              android:padding="20dp"
              android:paddingBottom="10dp"
              android:textColor="#000000"
              android:textColorHint="#d3d3d3"
              android:textSize="14sp"
              android:topLeftRadius="10dp"
              android:topRightRadius="10dp"/>
              <ImageButton
                  android:id="@+id/imgshowhide1"
                  android:layout_width="40dp"
                  android:layout_height="20dp"
                  android:layout_marginTop="20dp"
                  android:layout_marginRight="10dp"
                  android:background="@drawable/showpass"
                  android:layout_alignRight="@+id/etpass1"/>
          </RelativeLayout>    

 boolean show=true;
 //on image click inside password do this
 if(show){
                imgshowhide2.setBackgroundResource(0);
                imgshowhide2.setBackgroundResource(R.drawable.hide);
                etpass2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                etpass2.setSelection(etpass2.getText().length());

                show=false;
            }else{
                imgshowhide2.setBackgroundResource(0);
                imgshowhide2.setBackgroundResource(R.drawable.showpass);
                //etpass1.setInputType(InputType.TYPE_TEXT);
                etpass2.setInputType(InputType.TYPE_CLASS_TEXT |
                        InputType.TYPE_TEXT_VARIATION_PASSWORD);
                etpass2.setSelection(etpass2.getText().length());
                show=true;
            }

显示:

editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

隐藏:

editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

在这些之后,光标被重置,所以:

editText.setSelection(editText.length());