Android 在TextInputLayout中切换密码时的回调
我已经为我的密码字段选择了TextInputItemText,以便使用切换密码功能 以下是我的xml代码:Android 在TextInputLayout中切换密码时的回调,android,android-textinputlayout,android-textinputedittext,Android,Android Textinputlayout,Android Textinputedittext,我已经为我的密码字段选择了TextInputItemText,以便使用切换密码功能 以下是我的xml代码: <com.google.android.material.textfield.TextInputLayout android:layout_width="@dimen/login_width" android:layout_height="wrap_content" android:layout_gravity="cen
<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
上调用setEndiconClickListener
:
textInputLayout.setEndIconOnClickListener { v ->
// Layout changes here
}
但是,这将删除负责切换密码转换方法的单击侦听器。我建议只复制PasswordToggleEndIconDelegate
中的click listener代码,并在上面添加您自己的功能:
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
}
编辑:此方法仅在材料库版本
1.1.0-alpha04
及以后版本中可用,截至撰写之时,1.1.0
仍处于测试阶段。它与您的问题无关,但app:passwordToggleDrawable=“@drawable/password\u toggle\u drawable”
和app:passwordToggleEnabled=“true”
不推荐使用。使用app:endIconMode=“password\u toggle”。谢谢Sandi,我会查出来的。