在 TextInputLayout 中切换密码的回调
Callback on toggling password in TextInputLayout
我为我的密码字段选择了 TextInputEditText,以便使用切换密码功能。
这是我的 xml 代码:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="@dimen/login_width"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/password_margin_top"
app:hintEnabled="false"
app:passwordToggleDrawable="@drawable/password_toggle_drawable"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/my_login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:hint="@string/password"
android:inputType="textPassword"
android:nextFocusDown="@+id/my_login_login"
android:padding="@dimen/field_padding" />
</com.google.android.material.textfield.TextInputLayout>
我必须对切换密码进行一些其他布局更改。在 TextInputLayout 中是否有任何可用的回调?
您可以在 TextInputLayout
:
上拨打 setEndIconOnClickListener
textInputLayout.setEndIconOnClickListener { v ->
// Layout changes here
}
但是,这会删除负责切换密码转换方法的点击侦听器。我建议只复制 PasswordToggleEndIconDelegate
中的点击侦听器代码并在顶部添加您自己的功能:
textInputLayout.setEndIconOnClickListener {
val editText: EditText? = textInputLayout.editText
// Store the current cursor position
val selection = editText?.selectionEnd ?: 0
// Check for existing password transformation
val hasPasswordTransformation = editText?.transformationMethod is PasswordTransformationMethod;
if (hasPasswordTransformation) {
editText?.transformationMethod = null
} else {
editText?.transformationMethod = PasswordTransformationMethod.getInstance()
}
// Restore the cursor position
editText?.setSelection(selection)
// Add additional functionality here
}
编辑:此方法仅在 material 库版本 1.1.0-alpha04
之后可用,截至撰写本文时,1.1.0
仍处于测试阶段。
我为我的密码字段选择了 TextInputEditText,以便使用切换密码功能。
这是我的 xml 代码:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="@dimen/login_width"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="@dimen/password_margin_top"
app:hintEnabled="false"
app:passwordToggleDrawable="@drawable/password_toggle_drawable"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/my_login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:hint="@string/password"
android:inputType="textPassword"
android:nextFocusDown="@+id/my_login_login"
android:padding="@dimen/field_padding" />
</com.google.android.material.textfield.TextInputLayout>
我必须对切换密码进行一些其他布局更改。在 TextInputLayout 中是否有任何可用的回调?
您可以在 TextInputLayout
:
setEndIconOnClickListener
textInputLayout.setEndIconOnClickListener { v ->
// Layout changes here
}
但是,这会删除负责切换密码转换方法的点击侦听器。我建议只复制 PasswordToggleEndIconDelegate
中的点击侦听器代码并在顶部添加您自己的功能:
textInputLayout.setEndIconOnClickListener {
val editText: EditText? = textInputLayout.editText
// Store the current cursor position
val selection = editText?.selectionEnd ?: 0
// Check for existing password transformation
val hasPasswordTransformation = editText?.transformationMethod is PasswordTransformationMethod;
if (hasPasswordTransformation) {
editText?.transformationMethod = null
} else {
editText?.transformationMethod = PasswordTransformationMethod.getInstance()
}
// Restore the cursor position
editText?.setSelection(selection)
// Add additional functionality here
}
编辑:此方法仅在 material 库版本 1.1.0-alpha04
之后可用,截至撰写本文时,1.1.0
仍处于测试阶段。