一个按钮应该在一分钟内只点击两次,我应该如何检查?
A button should be clicked only two times in a minute, how should I check it?
一个按钮应该在一分钟内只被点击两次。例如,如果用户在 50. second 第三次点击,应用程序将通过 toast 消息警告用户。此外,对于第 4 次和第 2 次点击,也应该有 60 秒。我试着为此写了一个方法,
int counter = 0;
long counterOne = 0;
long counterTwo = 0;
long counterThree = 0;
private boolean checkTime() {
counter++;
if (counter == 1) {
counterOne = System.currentTimeMillis();
}
if (counter == 2) {
counterTwo = System.currentTimeMillis();
}
if (counter == 3) {
counterThree = System.currentTimeMillis();
}
if (counterThree != 0) {
if (counterThree < (counterOne + (60 * 1000))) {
counter--;
return false;
}
}
if (counter == 1 || counter == 2 || counterThree > (counterOne + (60 * 1000))) {
if (counter == 3) {
counter = 1;
counterOne = counterThree;
}
return true;
}
return false;
}
我想将其用作;
img_number_search.setOnClickListener(view -> {
if (checkTime()) {
// TODO
}
else {
Toast.makeText(context, "You can use this property only two times in a minute", Toast.LENGTH_SHORT).show();
}
});
我建议您启动一个每 60 秒重置一次的计数器。
尝试这样的事情:
private int counter=0;
private Long timeSinceLastClick = 0;
private boolean checkTime(){
if(counter == 0){
timeSinceLastClicked = System.currentTimeMillis;
} // if our counter is zero, we start a timer.
counter++;
if(counter < 2){ // if our counter is less than two , then we return true.
return true;
}else{ // Otherwise we need to check if 60 seconds passed.
long currentTime = System.currentTimeMillis();
long timeDifference = (currentTime-timeSinceLastClick)/1000;
if(timeDifference > 60){ // been more than 60 seconds.
counter =0;
timeSinceLastClicked = System.currentTimeMillis;
counter++;
return true;
}else{
return false;
}
}
}
您可以使用 Thread 来执行此操作。
int counter=0; //globally declared
在 OnClick 方法中
if(counter==0)
{
Thread th=new Thread(new callthread());
th.start();
}
counter++;
if(counter<=2)
{
//TODO
}
else
{
Toast.makeText(context, "You can use this property only two times in a minute", Toast.LENGTH_SHORT).show();
}
调用线程方法
public class callthread implements Runnable{
@Override
public void run() {
try {
Thread.sleep(Interval time in millisec);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter=0;
}
}
只保留一个计数器变量。然后使用这段代码,当计数器的值变为 2
new Handler().postDelayed(new Runnable()
{
public void run()
{
img_number_search.setEnabled(false);
}
}, 60000 //Specific time in milliseconds
);
此代码禁用按钮 1 分钟。
一个按钮应该在一分钟内只被点击两次。例如,如果用户在 50. second 第三次点击,应用程序将通过 toast 消息警告用户。此外,对于第 4 次和第 2 次点击,也应该有 60 秒。我试着为此写了一个方法,
int counter = 0;
long counterOne = 0;
long counterTwo = 0;
long counterThree = 0;
private boolean checkTime() {
counter++;
if (counter == 1) {
counterOne = System.currentTimeMillis();
}
if (counter == 2) {
counterTwo = System.currentTimeMillis();
}
if (counter == 3) {
counterThree = System.currentTimeMillis();
}
if (counterThree != 0) {
if (counterThree < (counterOne + (60 * 1000))) {
counter--;
return false;
}
}
if (counter == 1 || counter == 2 || counterThree > (counterOne + (60 * 1000))) {
if (counter == 3) {
counter = 1;
counterOne = counterThree;
}
return true;
}
return false;
}
我想将其用作;
img_number_search.setOnClickListener(view -> {
if (checkTime()) {
// TODO
}
else {
Toast.makeText(context, "You can use this property only two times in a minute", Toast.LENGTH_SHORT).show();
}
});
我建议您启动一个每 60 秒重置一次的计数器。
尝试这样的事情:
private int counter=0;
private Long timeSinceLastClick = 0;
private boolean checkTime(){
if(counter == 0){
timeSinceLastClicked = System.currentTimeMillis;
} // if our counter is zero, we start a timer.
counter++;
if(counter < 2){ // if our counter is less than two , then we return true.
return true;
}else{ // Otherwise we need to check if 60 seconds passed.
long currentTime = System.currentTimeMillis();
long timeDifference = (currentTime-timeSinceLastClick)/1000;
if(timeDifference > 60){ // been more than 60 seconds.
counter =0;
timeSinceLastClicked = System.currentTimeMillis;
counter++;
return true;
}else{
return false;
}
}
}
您可以使用 Thread 来执行此操作。
int counter=0; //globally declared
在 OnClick 方法中
if(counter==0)
{
Thread th=new Thread(new callthread());
th.start();
}
counter++;
if(counter<=2)
{
//TODO
}
else
{
Toast.makeText(context, "You can use this property only two times in a minute", Toast.LENGTH_SHORT).show();
}
调用线程方法
public class callthread implements Runnable{
@Override
public void run() {
try {
Thread.sleep(Interval time in millisec);
} catch (InterruptedException e) {
e.printStackTrace();
}
counter=0;
}
}
只保留一个计数器变量。然后使用这段代码,当计数器的值变为 2
new Handler().postDelayed(new Runnable()
{
public void run()
{
img_number_search.setEnabled(false);
}
}, 60000 //Specific time in milliseconds
);
此代码禁用按钮 1 分钟。