c# - 在 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);
}
}
}
}
解决方案
您可以使用一个参数来记录点击次数,然后根据该次数做某事,我认为您需要定义一个间隔来确定点击是否有效,您可以检查以下内容:
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
你可以在街区里做点什么。
推荐阅读
- asp.net - 过滤下拉列表选项服务器端 asp.net
- python - 拆分多个(嵌套)python 子正则表达式定义
- python - Visual Studio Code 快速修复 & python
- javascript - 通过另一个数据属性作为标识符获取元素的数据属性
- python-3.x - matplotlib 动画在循环执行的文件更新中不起作用
- python - 将自定义生成器添加到 spaCy 的类
- android - 授予位置权限时,Android O 设备中的“位置方法”不会更改
- ag-grid - rowNode.setDataValue 方法的性能
- asp.net - 检查对 SOAP/REST Web 服务的访问而不调用它
- assembly - 为什么换行符会自动添加到字符串中?