首页 > 解决方案 > 在 Xamarin Android 中创建一个需要点击 3 次的按钮

问题描述

在 Xamarin Android 中创建一个需要单击 3 次的按钮。

到目前为止有以下,但不工作

我已经在主要活动中创建了这个,并在表单上使用了一个按钮

但是,当单击按钮时(单击一次时确实有效)现在不起作用

public async void DepositAsync(object sender, EventArgs eventArgs)
     int counter = 0;

    {
        if (Security.IsDeposited) return;
        bool result = await Security.Deposit(EnteredAmount.Text);

        {



            if (counter == 1) 

            {

            Toast.MakeText(this, "Please click button twice", ToastLength.Long).Show();

            }

            else if (counter == 2)

            {
                Toast.MakeText(this, "Triple click to confirm deposit", ToastLength.Long).Show();
            }


           else if (counter == 3) 

           {
                if (result)
                {
                    Toast.MakeText(this, "Deposit Successfully", ToastLength.Long).Show();
                    StartActivity(new Intent(this, typeof(MainActivity)));
                    ValidationText.Text = "Success!";
                    ValidationText.SetTextColor(new Color(20, 230, 20));
                }
                else
                {
                    Animation shake = AnimationUtils.LoadAnimation(this, Resource.Animation.shake);
                    ValidationText.Text = "Cannot Deposit...please try again";
                    ValidationText.SetTextColor(new Color(230, 20, 20));
                    ValidationText.StartAnimation(shake);
                    ConfirmDeposit.StartAnimation(shake);
                    EnteredAmount.StartAnimation(shake);
                }

            }

        }
    }

标签: c#androidxamarinbutton

解决方案


您可以使用一个参数来记录点击次数,然后根据该次数做某事,我认为您需要定义一个间隔来确定点击是否有效,您可以检查以下内容:

  private int clickcount = 0; // record the click count
  private long clickTime;  //the time you click
  private long preClickTime; //the last click timev
  private long interval  = 2000; //the valid interval

  Button button = FindViewById<Button>(Resource.Id.button);

  button.Click += delegate
       {
           clickcount++;
           clickTime = Java.Lang.JavaSystem.CurrentTimeMillis();

           if (clickcount == 1)
           {
               preClickTime = clickTime;
               Toast.MakeText(this, "Please click button twice", ToastLength.Long).Show();
           }
           else
           {

               if ((clickTime - preClickTime) < interval)
               {
                   preClickTime = clickTime;
                   switch (clickcount)
                   {
                       case 2:
                           Toast.MakeText(this, "Triple click to confirm deposit", ToastLength.Long).Show();
                           break;
                       case 3:
                           preClickTime = 0;
                           clickcount = 0;
                           Toast.MakeText(this, "Deposit Successfully", ToastLength.Long).Show();
                           break;
                   }

               }
               else
               {
                   clickcount = 1;
                   preClickTime = clickTime;
                   Toast.MakeText(this, "Please click button twice", ToastLength.Long).Show();
                   return;
               }
           }
         };

case 3你可以在街区里做点什么。


推荐阅读