Reputation: 453
I'm working on an app through xamarin and I'm using a Button to check registration fields.
Button sendToRegisterButton = (Button)FindViewById(Resource.Id.registerbtn);
sendToRegisterButton.Touch += delegate { CheckFields(); };
So when the button is touched, I check for a boolean touched
to see if the button has already been pressed.
I set touched to true if the if(!touched)
passes. I let the code run and at the end of the if statement I set touched to false again. Now, when I test this and press the register button on my phone, the checkfields function is still called multiple times.
Relevant Code:
private void CheckFields()
{
if (!touched)
{
touched = true;
//Rest of code here... checking the username and password fields etc
touched = false;
}
}
EDIT: I tried disabling the button and reenabling it afterwards, didn't work unfortunately
private void CheckFields()
{
sendToRegisterButton.Enabled = false;
//Rest of code here... checking the username and password fields etc
sendToRegisterButton.Enabled = true;
}
EDIT2: Since people were asking, this is the whole class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using ArcheologieApplication.Code;
using System.Text.RegularExpressions;
using ArcheologieApplication.Code.Queries;
namespace ArcheologieApplication
{
[Activity(Label = "ArcheologieApplication", MainLauncher = true, Theme = "@android:style/Theme.Black.NoTitleBar.Fullscreen")]
class RegisterActivity : Activity
{
private Button sendToRegisterButton;
PlayerInfoQuery playerQuery = new PlayerInfoQuery();
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.RegisterLayout);
SetupButtons();
}
private void SetupButtons()
{
Button loginButton = (Button)FindViewById(Resource.Id.signinbtn);
loginButton.Touch += delegate { StartActivity(typeof(LoginActivity)); };
sendToRegisterButton = (Button)FindViewById(Resource.Id.registerbtn);
sendToRegisterButton.Touch += delegate { CheckFields(); };
}
private void CheckFields()
{
sendToRegisterButton.Enabled = false;
RegexUtilities checkMail = new RegexUtilities();
var username = FindViewById<EditText>(Resource.Id.regnametxt);
var userEmail = FindViewById<EditText>(Resource.Id.regemailtxt);
var userPassword = FindViewById<EditText>(Resource.Id.regpasstxt);
var userPasswordConfirm = FindViewById<EditText>(Resource.Id.regconfirmtxt);
PlayerInfo player = new PlayerInfo();
bool nameBool = false;
bool emailBool = false;
bool passBool = false;
if (string.IsNullOrEmpty(username.Text))
{
Toast.MakeText(this, "Invalid Username", ToastLength.Short).Show();
}
else
{
player.PlayerName = username.Text;
nameBool = true;
}
if (string.IsNullOrEmpty(userEmail.Text))
{
//TODO: Verify Email adress for valid email.
Toast.MakeText(this, "Invalid Email Adress", ToastLength.Short).Show();
}
else
{
if (checkMail.IsValidEmail(userEmail.Text))
{
player.PlayerEmail = userEmail.Text;
emailBool = true;
}
else
{
Toast.MakeText(this, "Invalid Email Adress", ToastLength.Short).Show();
}
}
if (string.IsNullOrEmpty(userPassword.Text) || string.IsNullOrEmpty(userPasswordConfirm.Text))
{
Toast.MakeText(this, "Invalid Password Invalid", ToastLength.Short).Show();
}
else
{
if (userPassword.Text != userPasswordConfirm.Text)
{
Toast.MakeText(this, "Passwords not the same", ToastLength.Short).Show();
}
else
{
PasswordHasher hasher = new PasswordHasher();
byte[] saltBytes = hasher.GenerateRandomSalt(PasswordHasher.saltByteSize);
string saltString = Convert.ToBase64String(saltBytes);
string passwordHash = hasher.PBKDF2_SHA256_GetHash(userPassword.Text, saltString,
PasswordHasher.iterations, PasswordHasher.hashByteSize);
Console.WriteLine("SALT: " + saltString.Length);
Console.WriteLine("HASH: " + passwordHash.Length);
string hashedPwd = saltString + passwordHash;
player.PlayerPassword = hashedPwd;
passBool = true;
}
}
//If everything is correct insert into database
if (nameBool && emailBool && passBool)
{
player.PlayerPoints = 0; //Standard is 0
playerQuery.Insert(player);
Toast.MakeText(this, "Thank you for registering, please login", ToastLength.Long).Show();
StartActivity(typeof(LoginActivity));
}
sendToRegisterButton.Enabled = true;
}
}
}
Upvotes: 1
Views: 895
Reputation: 12170
The Touch
event handler is called for touch down, up and motion events.
It looks like you are trying to handle a click on the buttons; consider using the Clicked
event callback instead (which will only fire when the user touches down and then releases your button):
private void SetupButtons()
{
Button loginButton = (Button)FindViewById(Resource.Id.signinbtn);
loginButton.Clicked += delegate { StartActivity(typeof(LoginActivity)); };
sendToRegisterButton = (Button)FindViewById(Resource.Id.registerbtn);
sendToRegisterButton.Clicked += delegate { CheckFields(); };
}
Upvotes: 5