【问题标题】:Android EditText for password with android:hint带有 android:hint 的 Android EditText 密码
【发布时间】:2011-07-20 06:25:31
【问题描述】:

刚刚注意到 android:password 已被弃用,我们应该使用 android:inputType。通过在我的 xml 中设置来试验它

android:inputType="textPassword" 

确实是这样的

android:password="true" 

对于 EditText,但似乎如果我使用 android:inputType,android:hint 将不起作用。 EditText 将为空白。使用 android:password 和 android:hint 时没有这样的问题。我在这里遗漏了一些关于 android:inputType 的内容吗?

【问题讨论】:

  • android:password 已弃用 :(

标签: android xml passwords android-edittext hint


【解决方案1】:

提示文字不加粗,我尝试下面的代码。

当我更改inputtype=email 时,其他编辑文本为粗体。但是当我将输入类型更改为password时,提示是正常的。

我需要将提示文本加粗,我的代码是:

 <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="56dp"
            app:theme="@style/Widget.Design.TextInputLayout"
            >
                <EditText
                    android:id="@+id/login_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Password"
                    android:textStyle="bold"
                    android:inputType="textPassword"
                    android:textColor="@color/White"
                    style="@style/Base.TextAppearance.AppCompat.Small"
                    android:drawableLeft="@drawable/ic_password"
                    android:drawablePadding="10dp"
                    />
            </android.support.design.widget.TextInputLayout>

【讨论】:

    【解决方案2】:

    这是你的答案:

    inputType 有不同的类别,所以我使用的密码是textPaswword

    <EditText
        android:inputType="textPassword"
        android:id="@+id/passwor"
        android:textColorHint="#ffffff"
        android:layout_marginRight="15dp"
        android:layout_marginLeft="15dp"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:hint="********"
        />
    

    【讨论】:

      【解决方案3】:

      这是你的答案。我们可以同时使用两者。正如我使用的那样,它们工作正常。代码如下:

      <EditText
          android:id="@+id/edittext_password_la"
          android:layout_below="@+id/edittext_username_la"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_margin="15dip"
          android:inputType="textPassword"
          android:hint="@string/string_password" />
      

      这会对你有所帮助。

      【讨论】:

        【解决方案4】:

        使用

        正确显示提示
        android:inputType="textPassword"
        

        android:gravity="center"
        

        如果你也设置了

        android:ellipsize="start"
        

        【讨论】:

        • 谢谢。令人费解的是,为什么 ellipsize 会影响提示的可见性。我没有看到这背后的思路来帮助我理解这一点。有什么建议吗?
        • 没有 :) 这是一个平台错误 afaik,尽管它已经存在多年了。
        【解决方案5】:

        如果你设置了

        android:inputType="textPassword"
        

        此属性,如果您提供数字作为密码示例“1234567”,它会将其视为“123456/”,不采用第七个字符。 这就是为什么使用

        而不是这种方法
        android:password="true"
        

        允许您输入任何类型的密码而不受任何限制的属性。

        如果你想提供提示使用

        android:hint="hint text goes here"
        

        示例:

        android:hint="password"
        

        【讨论】:

          【解决方案6】:

          只是偶然发现了答案。 android:inputType="textPassword"android:hint 一起使用,与 android:password 相同。唯一的区别是当我使用android:gravity="center" 时,如果我使用android:inputType,则不会显示提示。结案!

          【讨论】:

            【解决方案7】:

            我遇到了同样的问题并找到了解决方案:

            以前的代码:

                <EditText 
                android:layout_height="wrap_content"
                android:gravity="center" 
                android:inputType ="password" 
                android:layout_width="fill_parent"
                android:id="@+id/password" 
                android:hint="password" 
                android:maxLines="1">
                </EditText>
            

            解决方案:

            <EditText 
            android:layout_height="wrap_content"
            android:gravity="center" 
            android:password="true" 
            android:layout_width="fill_parent"
            android:id="@+id/password" 
            android:hint="password" 
            android:maxLines="1">
            </EditText>
            

            之前的代码没有显示“提示”,但是当我将其更改为最后一个时,它开始显示...

            希望这对某人有所帮助...

            【讨论】:

              【解决方案8】:

              实际上,我发现如果您将 android:gravity="center" 放在 xml 行的末尾,提示文本与 android:inputType="textVisiblePassword" 一起显示得很好

              【讨论】:

              • 这很有趣。感谢分享!
              【解决方案9】:

              android:hint="Enter your question" 或类似的东西必须有效。我正在使用带有 EditText 的相对布局作为 如果你想使用密码,说 android:inputType="textPassword" 来隐藏字符和 "textVisiblePassword" 来显示你输入的密码。

              【讨论】:

              • 嗨拉什米,感谢您的回答。我想知道你是否错过了我的问题。我期待一个带有 android:hint="password" 和 android:inputType="textPassword" 的 EditText 在我的 Activity 首次加载时显示提示“密码”,但在用户开始输入密码时表现得像密码字段。但是,我之前使用 android:password 观察到的这种特殊行为不再与 android:inputType 相同。这是我的问题。在旁注中,“textVisiblePassword”有什么用?我不确定是否需要这种特殊用法。
              猜你喜欢
              • 2013-03-25
              • 2021-06-26
              • 2021-06-17
              • 1970-01-01
              • 2021-03-14
              • 1970-01-01
              • 2019-08-25
              • 2021-09-12
              • 2011-11-20
              相关资源
              最近更新 更多