【问题标题】:How to place an ImageView beside EditText password input field如何在 EditText 密码输入字段旁边放置一个 ImageView
【发布时间】:2019-12-25 16:23:23
【问题描述】:

我正在尝试使用 EditText TextView 在密码字段中进行密码可见性切换(我不想使用 android 支持 TextInputLayout)

一切正常,但我如何在密码 EditText 字段旁边的正确位置调整 ImageView。

这就是我现在拥有的

这就是我想要的

我希望图像位于密码 EditText 字段内的那个位置。

我的布局文件

 <EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/etPassword"
    android:layout_marginRight="20dp"
    android:inputType="textPassword"
    android:layout_marginLeft="20dp"
    android:layout_marginBottom="20dp"
    android:paddingLeft="20dp"
    android:background="@drawable/input_border"
    android:hint="Your Password" />

<ImageView
    android:id="@+id/show_pass_btn"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dp"
    android:alpha=".5"
    android:onClick="ShowHidePass"
    android:padding="5dp"
    android:src="@drawable/eye_show" />

【问题讨论】:

  • I dont want to use the android support TextInputLayout) 为什么?很简单。
  • @IntelliJAmiya 看起来与我喜欢的普通 editText 字段非常不同。
  • 我已经完成了 java 代码,只是想找到一种将图像放在 editText 字段旁边的方法
  • @Ibramazin 您正在寻找的正是一个 TextInputLayout。只需使用正确的样式
  • TextInputLayout 有这个功能stackoverflow.com/a/44173975/4017501

标签: java android xml


【解决方案1】:

我想提出两个解决方案。我自己正在使用第二个

首先:通过向 EditText 添加可绘制对象

你可以像这样向 EditText 添加一个图标:

    <EditText
        ...
        android:drawableEnd="@drawable/ic_settings_24dp"
        ...
    />

然后像这样添加一个监听器:Handling click events on a drawable within an EditText

二:通过扩展TextInputLayout

我遇到了同样的问题,所以我所做的是创建一个扩展 TextInputLayout 的视图类。这样,默认情况下材质主题不会应用填充框样式,您仍然可以获得所有 TextInputLayout 优点。

import android.content.Context
import android.util.AttributeSet
import com.google.android.material.textfield.TextInputLayout

class CustomTextInputLayout @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): TextInputLayout(context, attrs, defStyleAttr) {

}
    <com.passwordstore.android.ui.views.CustomTextInputLayout
        android:id="@+id/til_notes"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/notes"
        app:endIconMode="clear_text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/btn_generate_password"
        style="@style/BoxTextInputLayout">
        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/poppins_medium" />
    </com.passwordstore.android.ui.views.CustomTextInputLayout>

结果如下

Old EditText design with TextInputLayout

【讨论】:

  • 不知道为什么要重新发明轮子 - TextInputLayout 开箱即用
  • 是的,我仍在使用 TextInputLayout,但是它将默认的 FilledBox 样式应用于 EditText。我不知道有什么方法可以禁用该样式,所以我将 TextInputLayout 包装在一个自定义类中。这样 AppTheme 就不会向其中添加材质样式。
  • 没有理由以这种方式扩展 TextInputLayout。换个风格就行了
  • 尝试了 TextInputLayout 的所有可用样式,但似乎都没有。你知道哪种风格会回归旧的 EditText 设计吗?
【解决方案2】:

您可以在TextInputLayout 中使用app:passwordToggleEnabled="true",如果您还需要自定义图标使用,"app:passwordToggleDrawable="

      <com.google.android.material.textfield.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="50dp"
                            app:passwordToggleEnabled="true">
        
                            <com.google.android.material.textfield.TextInputEditText
                                android:id="@+id/ed_password"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"  
                                android:gravity="center_vertical"
                                android:hint="Password"
                                android:imeOptions="actionNext"
                                android:inputType="textPassword"
                                android:textSize="16sp"
                                android:padding="10dp" />
        
        
                        </com.google.android.material.textfield.TextInputLayout>

【讨论】:

    【解决方案3】:
    <EditText
        android:id="@+id/loginPassword"
        android:layout_width="325dp"
        android:layout_height="53dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/et_style"
        android:hint="Password"
        android:inputType="textPassword"
        android:paddingLeft="8dp"
        android:textColorHint="@color/colorOrange"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/loginUsername" />
    
    <ImageView
        android:id="@+id/password_toggle_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="12dp"
        android:layout_marginRight="12dp"
        app:layout_constraintBottom_toBottomOf="@id/loginPassword"
        app:layout_constraintEnd_toEndOf="@+id/loginPassword"
        app:layout_constraintTop_toTopOf="@+id/loginPassword"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/ic_baseline_visibility_24"
        tools:ignore="VectorDrawableCompat" />
    

    【讨论】:

    • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-12-26
    • 1970-01-01
    相关资源
    最近更新 更多