如何解决KeyPress 不适用于密码 EditText 类型
为什么此代码在 Xamarin.Android 上不起作用?如果输入密码字段,它将更改按钮的 enable
属性。
EditText edit_user_001 = FindViewById<EditText>(Resource.Id.id_edit_user_001);
EditText edit_password_001 = FindViewById<EditText>(Resource.Id.id_edit_password_001);
Button btn_login_001 = FindViewById<Button>(Resource.Id.id_btn_login_001);
edit_password_001.KeyPress += (sender,e) => {
if (edit_user_001.Text.ToString().Equals("") | edit_password_001.Text.ToString().Equals("")){
btn_login_001.Enabled = false;
} else {
btn_login_001.Enabled = true;
}
};
这是.xml:
<TextView
android:id="@+id/id_txt_user_001"
android:text="@string/txt_user_001"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/id_edit_user_001"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10.0dp"
android:inputType="text"/>
<TextView
android:text="@string/txt_password_001"
android:id="@+id/id_txt_password_001"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/id_edit_password_001"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10.0dp"
android:inputType="textPassword"/>
android:inputType="textPassword
会产生冲突。如果我更改为 android:inputType="text
它工作正常。有没有替代方案或解决方案?
解决方法
android:inputType="textPassword 会产生冲突。如果我更改为 android:inputType="text,它工作正常。有没有替代方案或解决方案?
是的,我在设置 EditText android:inputType="textPassword"
时遇到了相同的 KeyPress 问题。
您可以使用 TextChanged
方法来达到相同的效果。
private void Password_TextChanged(object sender,Android.Text.TextChangedEventArgs e)
{
if (username.Text.Equals("") || password.Text.Equals(""))
{
button.Enabled = false;
}
else
{
button.Enabled = true;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。