-1

I'm looking how to make this funcionality on C# to Android Xamarin. It is to enable the login buttom only when the user type his user name and password. Other wise, the button shuold be disable.

My C# code doesn't work

            String edit_user_001 = (String) FindViewById<EditText>(Resource.Id.id_edit_user_001);
            String edit_password_001 = (String) FindViewById<EditText>(Resource.Id.id_edit_password_001);
            Button btn_login_001 = FindViewById<Button>(Resource.Id.id_btn_login_001);

            if (edit_user_001.Equals("") || edit_user_001.Equals("")){
                btn_login_001.Enabled = false;
            } else {
                User user = new User(edit_user_001, edit_user_001);
                btn_login_001.Enabled = true;
                btn_login_001.Click += (sender, e) => {
                    login(user);
                };
            }

Someone knows what I am doing wron?

David Zomada
  • 167
  • 1
  • 5
  • 22

1 Answers1

2

you are trying to cast your EditText controls to string which will not work

String edit_user_001 = (String) FindViewById<EditText>(Resource.Id.id_edit_user_001);

instead

EditText edit_user_001 = FindViewById<EditText>(Resource.Id.id_edit_user_001);

then use it's Text property to access the string value

if (edit_user_001.Text.Equals("") || edit_user_001.Text.Equals(""))
        
        
Jason
  • 86,222
  • 15
  • 131
  • 146
  • If I do that, the button still disabled although I type into both inputs – David Zomada Mar 22 '21 at 11:34
  • 1
    where is this code located? If you only execute it when the page loads and the inputs are empty, of course nothing will happen. You need to execute it when the user enters text using the `KeyPress` event – Jason Mar 22 '21 at 11:51
  • It is located onCreate. Should I change to another function? – David Zomada Mar 22 '21 at 11:54
  • yes, read my last comment about using `KeyPress` – Jason Mar 22 '21 at 11:57
  • I'm looking for an example but I can't find an understable one (I'm Android noob). Would you mind to post one example of the KeyPress? – David Zomada Mar 22 '21 at 12:16
  • 1
    search "xamarin edittext" and the first hit is a link to the official docs, which contains an example of using `KeyPress`. https://learn.microsoft.com/en-us/xamarin/android/user-interface/controls/edit-text – Jason Mar 22 '21 at 12:22
  • Thank you. I was looking for "KeyPress Xamarin". – David Zomada Mar 22 '21 at 12:24