Button.Touch 仅触摸一次时会触发多次

Pim*_*ert 1 c# events delegates xamarin.android xamarin

我正在通过 xamarin 开发一个应用程序,并且使用按钮来检查注册字段。

Button sendToRegisterButton = (Button)FindViewById(Resource.Id.registerbtn);
sendToRegisterButton.Touch += delegate { CheckFields(); };
Run Code Online (Sandbox Code Playgroud)

因此,当触摸按钮时,我会检查布尔值touched以查看按钮是否已被按下。

如果通过,我将感动设置为真if(!touched)。我让代码运行,并在 if 语句末尾再次将 touch 设置为 false。现在,当我对此进行测试并按下手机上的注册按钮时,检查字段功能仍然会被多次调用。

相关代码:

    private void CheckFields()
    {
        if (!touched)
        {
            touched = true;
            //Rest of code here... checking the username and password fields etc
            touched = false;
        }
    }
Run Code Online (Sandbox Code Playgroud)

编辑:我尝试禁用该按钮并随后重新启用它,不幸的是没有工作

    private void CheckFields()
    {
        sendToRegisterButton.Enabled = false;
        //Rest of code here... checking the username and password fields etc
        sendToRegisterButton.Enabled = true;

    }
Run Code Online (Sandbox Code Playgroud)

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

mat*_*dev 5

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(); };
    }
Run Code Online (Sandbox Code Playgroud)