【问题标题】:Edittext password is always showing the passwordEdittext密码总是显示密码
【发布时间】:2017-08-22 20:42:57
【问题描述】:

我有一个 EditText 来输入密码,我将 inputType 设置为“textPassword”。但是当我在应用程序或模拟器中运行它时,密码是可见的。为什么显示密码而不是点?

<EditText
 android:id="@+id/etPassword"
 android:layout_width="match_parent"
 android:inputType="textPassword"
 android:layout_height="wrap_content"
 android:hint="@string/Password"
 android:layout_marginBottom="10dp"
 android:maxLength="15"
 android:background="@drawable/ss"/>

可绘制文件 ss:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="10dp"/>
<padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<solid android:color="#ffffffff"/>
<stroke android:width="1dp" android:color="#CCCCCC" />

完整布局:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:background="#ffffffff"
        android:gravity="center_vertical" >

        <ImageView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/foodstall"
         android:layout_gravity="center"
         android:layout_marginBottom="10dp"/>

        <LinearLayout
         android:layout_width="match_parent"
         android:orientation="vertical"
         android:layout_height="wrap_content"
         android:background="#ffffffff">

        <EditText
        android:id="@+id/etUsername"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/Username"
        android:maxLength="15"
        android:background="@drawable/ss"
        android:layout_marginBottom="10dp"/>

        <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:inputType="textPassword"
        android:layout_height="wrap_content"                                                     android:hint="@string/Password"
        android:layout_marginBottom="10dp"
        android:maxLength="15"
        android:background="@drawable/ss"
                android:text="askdjhkashdjasdksakdkaskdhkashkdhkashdkjhakshdkjhaskhdkj"/>

        <Button
        android:id="@+id/bLogin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/title_activity_login"
        android:background="@color/button"
        android:textColor="#ffffffff"
        android:layout_weight="1" />

        </LinearLayout>

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearlayout"
        android:background="@drawable/ss"
        android:layout_marginTop="10dp">

        <TextView
        android:id="@+id/tvRegisterLink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/Newuser"
        android:gravity="center"
        android:layout_weight="1"
        android:textStyle="bold"/>

        <TextView
        android:id="@+id/fpass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/title_activity_forgotpass"
        android:layout_weight="1"
        android:gravity="center"
        android:textStyle="bold"/>

        </LinearLayout>

        </LinearLayout>

【问题讨论】:

  • 你可以尝试删除maxLength,看看是否仍然存在同样的问题
  • @hrskrs - 没用。
  • 你能在edittext中展示你的UI吗?
  • @user2269164 尝试移除背景。另外,您可以尝试只输入 来测试它,这绝对可以。如果这不起作用,那就真的有问题了。
  • 我刚试过,效果很好。你在运行什么 API?

标签: android android-edittext


【解决方案1】:

1) 从您的 xml 文件中删除 android:inputType="textPassword" ,然后在 java 中设置它:

  EditText password = (EditText) findViewById(R.id.password_text);
  password.setTransformationMethod(new PasswordTransformationMethod());

使用这种方法,提示字体看起来不错,但是当您在该编辑字段中输入时,您不会在纯文本中看到每个字符,然后才会变成密码点。此外,全屏输入时,不会出现圆点,而是以明文形式显示密码。

2) 将 android:inputType="textPassword" 留在您的 xml 中。在 Java 中,密码方法:

  EditText password = (EditText) findViewById(R.id.register_password_text);
  password.setTransformationMethod(new PasswordTransformationMethod());

这种方法给出了密码点。

【讨论】:

  • @user2269164 试试这两种情况。
  • @user2269164 请接受它对其他人有用的答案。
【解决方案2】:

我已经测试过它工作正常

 <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:inputType="textWebPassword"
        android:layout_height="40dp"
        android:hint="Password"
        android:layout_marginBottom="10dp"
        android:maxLength="15" />

更改您的 inputType
android:inputType="textPassword"

android:inputType="textWebPassword"

【讨论】:

  • 密码仍然可见。
【解决方案3】:

我也遇到了这个问题,对我有用的是:

我将此 xml 属性添加到我的 EditText 中,它工作正常:

android:singleLine="true"

我知道它已被弃用,但 android:maxLines="1" 无法正常工作,尽管它可以替代 singleLine。

【讨论】:

    【解决方案4】:

    尝试在 EditText 中添加 android:password="true"。它应该可以工作。

        <EditText
          android:id="@+id/etPassword"
          android:layout_width="match_parent"
          android:inputType="textPassword"
          android:password="true"
          android:layout_height="wrap_content"
          android:hint="@string/Password"
          android:layout_marginBottom="10dp"
          android:maxLength="15"
          android:background="@drawable/ss"/>
    

    【讨论】:

    • 这应该可行,但已弃用。你真的不应该使用它,除非绝对没有其他方法。
    • @Amey - 我试过了,但它不起作用,它在 API 24 中也被弃用了。
    • @Amey 属性 android:password 已从 api 23 弃用。
    猜你喜欢
    • 2018-09-03
    • 2012-03-07
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    相关资源
    最近更新 更多