我正在通过xamarin开发应用程序,我正在使用Button检查注册字段。
Button sendToRegisterButton = (Button)FindViewById(Resource.Id.registerbtn);
sendToRegisterButton.Touch += delegate { CheckFields(); };
因此,当触摸按钮时,我会检查布尔值touched
以查看按钮是否已被按下。
如果if(!touched)
通过,我会将其设置为true。我让代码运行,并在if语句结束时再次设置为false。现在,当我测试它并按下手机上的注册按钮时,仍会多次调用checkfields功能。
相关守则:
private void CheckFields()
{
if (!touched)
{
touched = true;
//Rest of code here... checking the username and password fields etc
touched = false;
}
}
编辑:我尝试禁用该按钮并在之后重新启用它,不幸的是不能正常工作
private void CheckFields()
{
sendToRegisterButton.Enabled = false;
//Rest of code here... checking the username and password fields etc
sendToRegisterButton.Enabled = true;
}
EDIT2:由于人们在问,这是整个班级
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;
}
}
}
答案 0 :(得分:3)
为触地,向上和动作事件调用Touch
事件处理程序。
看起来你正试图点击按钮;考虑使用Clicked
事件回调(只有在用户触摸然后释放按钮时才会触发):
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(); };
}