Phong Nguyễn
Phong Nguyễn

Reputation: 810

getValue in EditText - XAMARIN ANDROID

After spend so much hour for find something so simple, finally I must go to here and need somebody help me.

How can I get the value from EditText in Xamarin, because it's always return null.

Here is my code:

Button loginBtn;
EditText username;
EditText password;
string user;
string pass;
   protected override void OnCreate(Bundle savedInstanceState)
        {
            RequestWindowFeature(Android.Views.WindowFeatures.NoTitle);
            base.OnCreate(savedInstanceState);
            // Create your application here
            SetContentView(Resource.Layout.LoginForm);

            loginBtn = FindViewById<Button>(Resource.Id.login);
            username = (EditText)FindViewById(Resource.Id.userName);
            password = (EditText)FindViewById(Resource.Id.password);

            user = username.Text.ToString();
            pass = password.Text.ToString();


            loginBtn.Click += (object sender, EventArgs e) =>
            {

                Toast.MakeText(this, user + " : " + pass, ToastLength.Long).Show();
            };
        }

Upvotes: 2

Views: 1555

Answers (1)

Yupi
Yupi

Reputation: 4470

You have to move your code inside button click event, because your editexts are in OnCreate method and they get value null as OnCreate is first method which is called (except if you give them some value in xml code) and you never call your editexts again to actually assign to them users input value. So I think this will resolve your problem:

loginBtn.Click += (object sender, EventArgs e) =>
        {
            user = username.Text.ToString();
            pass = password.Text.ToString();
            Toast.MakeText(this, user + " : " + pass, ToastLength.Long).Show();
        };

Upvotes: 1

Related Questions