我用通常的方法实现了一个带有密码字段的TextInputLayout。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:layout_marginRight="30dp"
android:autofillHints="password"
android:background="@drawable/edit_text_border"
android:hint="@string/password"
android:inputType="textPassword"
android:textColor="#000000"
android:textColorHint="#9e9e9e" />
</com.google.android.material.textfield.TextInputLayout>
我在依赖关系中添加了以下内容
implementation 'com.google.android.material:material:1.1.0'
目前还看不到 "eye-图标 "的可见性。是否有什么我遗漏的地方,或者我做错了什么?
确保使用一个 材料组件主题 在您的应用程序中,并使用像这样的布局。
<com.google.android.material.textfield.TextInputLayout
...
android:hint="@string/password"
app:endIconMode="password_toggle">
<com.google.android.material.textfield.TextInputEditText
...
android:inputType="textPassword"
/>
</com.google.android.material.textfield.TextInputLayout>
由于你使用的是一个 EditText
请检查 公文本:
说明: 一个文本字段由以下部分组成:
TextInputLayout
和aTextInputEditText
作为一个直接的孩子。使用EditText
作为孩子 或可但TextInputEditText
为文本字段提供了可访问性支持,并且允许TextInputLayout
对输入文本的视觉方面有更大的控制。如果一个EditText
使用时,请确保将其android:background
到@null
以致于TextInputLayout
可以对其设置适当的背景。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/pass"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Password"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>