【问题标题】:Show Password icon in TextInputLayout touching the base line在触及基线的 TextInputLayout 中显示密码图标
【发布时间】:2016-10-05 07:16:50
【问题描述】:

我在 TextInputLayout 中使用android:inputType="textPassword" 显示密码图标时遇到了一些问题。现在它显示像这样

如您所见,显示密码图标正在触及基线。 我想要这样的东西

你可以看到它们之间的差距。

实现TextInputLayout的xml代码如下:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/TextLabelSNA"
    app:layout_constraintTop_toBottomOf="@+id/mailSave"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginStart="16dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginLeft="16dp"
    android:layout_marginEnd="16dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginRight="16dp"
    android:id="@+id/tilOp"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintVertical_bias="0.0">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inputType="textPassword"
        android:hint="Old Password"
        android:ems="10"
        android:textColor="#0A0B12"
        android:id="@+id/oldPassword"/>
</android.support.design.widget.TextInputLayout>

使用的样式是:

<style name="TextLabelSNA" parent="Widget.Design.TextInputLayout">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@color/hintBlack</item>
    <item name="android:textSize">16sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/hintBlack</item>
    <item name="colorControlNormal">@color/hintBlack</item>
    <item name="colorControlActivated">@color/hintBlack</item>
</style>

请帮忙..!!!

【问题讨论】:

  • 您是否尝试增加字体大小?
  • @JoxTraex 将字体大小更改为 20sp 有效。但是我有一些限制,我不能将尺寸增加超过 16sp。还有其他解决方案吗?
  • 有什么可以添加文字而不是图标的吗?

标签: android material-design


【解决方案1】:

只需在你的文本输入布局中添加 app:passwordToggleEnabled="true"

<android.support.design.widget.TextInputLayout
            app:layout_widthPercent="90%"
            android:layout_height="wrap_content"
            android:hint="@string/password"
            android:textColorHint="@android:color/white"
            app:passwordToggleEnabled="true"
            app:passwordToggleTint="@android:color/white">

            <android.support.v7.widget.AppCompatEditText
                android:id="@+id/et_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPassword"
                android:textColor="@android:color/white"
                android:visibility="gone"
                 />


        </android.support.design.widget.TextInputLayout>

【讨论】:

  • 哇哦..超级回答哥
  • 这个应该被接受!它有效(至少对我来说):)
  • 由于 AndroidX“重构”,要使用的类包是“com.google.android.material.textfield”。而不是“android.support.design.widget”。
  • app:passwordToggleEnabled="true" 属性已弃用。在那个地方应该使用这个应用程序:endIconMode="password_toggle"。
【解决方案2】:
  <android.support.design.widget.TextInputLayout
        android:id="@+id/inputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:passwordToggleEnabled="true">
        <EditText
            android:id="@+id/txtpwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:drawableLeft="@drawable/lock"
            android:ems="10"
            android:hint="  Password"
            android:imeOptions="actionGo"
            android:inputType="textPassword"
            android:padding="10dp">

        </EditText>
    </android.support.design.widget.TextInputLayout>

【讨论】:

    【解决方案3】:

    这可能是其他人也注意到的错误并报告了here

    【讨论】:

    • 感谢您的澄清。
    • 实际上,我认为我将其缩小到文本大小。 edittext 中的默认文本大小为 18sp。使用该文本大小,我认为它看起来不错。如果我将文本大小设置为 15sp,就会发生这种情况。事后看来,我不认为这是一个错误,只是可绘制对象没有足够的空间。如果即使您自己没有设置文本大小,这种情况仍然发生,那么我不知道问题出在哪里。
    【解决方案4】:

    请检查您的支持库版本:24.2.0 如果是,库有错误 然后让它版本:24.2.1

    使用此代码。

    <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                    <EditText
                        android:id="@+id/password"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:hint="@string/prompt_password"
                        android:imeActionId="@+id/login"
                        android:imeActionLabel="@string/action_sign_in_short"
                        android:text="a1234"
                        android:imeOptions="actionUnspecified"
                        android:inputType="textPassword"
                        android:maxLines="1"
                        android:singleLine="true" />
    
                </android.support.design.widget.TextInputLayout>
    

    【讨论】:

    • 图标正在切割只是因为您的填充和边距部分。
    • 在我的布局中添加了实际查看内容。请检查更新的答案
    • Activity 主题在您的情况下可能会有所不同,使用 Theme.AppCompat.NoActionBar 作为父主题
    • 请检查支持库版本
    • 已经24.2了。 1 实际上这是一个已经报告的错误。
    【解决方案5】:

    如果您想删除错误图标 - (以防万一) 您可以在 TextInputLayout 中添加这一行。

    app:errorIconDrawable="@null"
    

    【讨论】:

      【解决方案6】:

      在你的 gradle 中添加一个库依赖

       dependencies {
          compile 'com.github.scottyab:showhidepasswordedittext:0.8'
      }
      

      然后添加远程maven url

       allprojects {
          repositories {
              maven {
                  url "https://jitpack.io"
              }
          }
      }`
      

      最后在你的xml中添加这个。就像普通的EditText一样

       <com.scottyab.showhidepasswordedittext.ShowHidePasswordEditText
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@+id/simplePassword"
              android:hint="Password"
              />
      

      详情请浏览此链接 Show/Hide Password EditText

      【讨论】:

      • 由于它现在包含在支持库中,因此无需使用该库。事实上,它已被弃用。见这篇文章:medium.com/@moyinoluwa/…
      • 此解决方案已弃用
      • 现在已经公布了。
      猜你喜欢
      • 2019-04-16
      • 2020-05-19
      • 2016-12-25
      • 2020-11-15
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      相关资源
      最近更新 更多