【问题标题】:KeyPress doesn't wokr for password EditText typesKeyPress 不适用于密码编辑文本类型
【发布时间】:2021-03-23 14:58:37
【问题描述】:

为什么此代码在 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,它工作正常。是否有替代方案或解决方案?

【问题讨论】:

  • “造成冲突”——这到底是什么意思?您是否收到错误或异常?具体是什么问题?
  • 这是一个错误。我无法使用我的键盘(在模拟器中)在输入中输入文本。另外,如果我使用模拟器的虚拟键盘,edit_password_001.Text.ToString().Equals("") 总是TRUE

标签: c# android xamarin android-edittext


【解决方案1】:

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;
        }
    }

【讨论】:

    猜你喜欢
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-20
    • 2014-05-27
    • 2019-04-08
    相关资源
    最近更新 更多