Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何检查按钮是否在2秒后单击?_Android_Server_Delay_Android View - Fatal编程技术网

Android 如何检查按钮是否在2秒后单击?

Android 如何检查按钮是否在2秒后单击?,android,server,delay,android-view,Android,Server,Delay,Android View,我有一个android应用程序,可以向服务器发送喜欢的内容 我想做的不是立即向服务器发送一个like,而是在用户仍然喜欢帖子的情况下,在2秒钟后发送 我喜欢空虚 public void rotationAnimation(ImageView button, int source1, int source2){ if(isLikeClicked){ button.setImageResource(source1); button.startAnimation

我有一个android应用程序,可以向服务器发送喜欢的内容

我想做的不是立即向服务器发送一个like,而是在用户仍然喜欢帖子的情况下,在2秒钟后发送

我喜欢空虚

public void rotationAnimation(ImageView button, int source1, int source2){
    if(isLikeClicked){
        button.setImageResource(source1);
        button.startAnimation(rotate_backward);
        isLikeClicked = false;
    }else{
        button.setImageResource(source2);
        button.startAnimation(rotate_forward);
        isLikeClicked = true;
    }

    ChangeLikeCount();

    if(isReadyToPost)
        if(!isLikeClicked){
            Like like = new Like();
            like.execute(ServerCons.HOST + "unlike");
        }else{
            Like like = new Like();
            like.execute(ServerCons.HOST + "like");
        }

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            // I thought the solution could be there
        }
    }, 2000);
}

您可以尝试创建一个休眠2秒的线程,之后它会检查用户是否仍然喜欢帖子,然后更新数据库,使用
isReadyToPost
标志执行此操作:

if(isReadyToPost){
  isReadyToPost=false;
}else{
  // try after 2 secs for next like
}
处理程序中。postDelayed
在2秒后将
isReadyToPost
更改为true:

  @Override
    public void run() {
        isReadyToPost=true;
    }
isReadyToPost
默认值为
true

尝试以下方法:

boolean isReadyToPost= false;
boolean liked = false;//control like click (witch)

public void onLikePressed() {
  if (liked && isReadyToPost) {
    sendLikeToServer();//send to server after 2 secs
    return;
   }

  this.isReadyToPost= false;
  Toast.makeText(this, "waiting for any dislike... in 2 secs", Toast.LENGTH_SHORT).show();
if (liked){
   new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
        isReadyToPost=true; 
        onLikePressed();                      
     }
   }, 2000);
 }//end if
} //end onlikepress