简体   繁体   English

Android-处理多次点击

[英]Android - handle multiple clicks

I have a functionality where user has a option to flip the camera from back to front and front to back on click of a button . 我具有一项功能,用户可以通过单击按钮来选择flip the camera从前向后flip the camera和从前向后flip the camera

Something like - 就像是 -

  rlFlipCamera.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            switchCamera();

        }
    });

Now if the user clicks it multiple times , the function tries to switch the camera multiple times which leads to lag/hang on some devices . 现在,如果用户multiple times单击它,该功能将尝试multiple times切换相机,这会导致某些设备出现lag/hang I want to prevent this using a logic - 我想使用逻辑来防止这种情况-

1. If the user clicks the button multiple times , i will store the count .
2. If the count is odd number , i will  switch the camera .
3. If the count is even number , i will not switch the camera .

I am not sure how to get the button click count . 我不确定如何获得按钮点击计数。 Please help. 请帮忙。

Thanks . 谢谢 。

This below sample may help you to solve the issue, 以下示例可以帮助您解决问题,

rlFlipCamera.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
      if(SystemClock.elapsedRealtime() - mLastClickTime<UtilityClass.CLICK_INTERVAL)  {
            return;
      }
       mLastClickTime = SystemClock.elapsedRealtime();
       switchCamera();

     }
    });

As per my understanding, here you dont need to maintain any count. 根据我的理解,这里您不需要维护任何计数。 If the user clicks the rlFlipCamera button, he wants to change the camera, he is not going to play with our application. 如果用户单击rlFlipCamera按钮,他想更改相机,则不会使用我们的应用程序。 If you follow the count then you maynot give right result to the user.Because,He don't know wether he clicked the button in odd or even numbers. 如果您按照计数进行操作,则可能无法为用户提供正确的结果。因为,他不知道是否单击了奇数或偶数按钮。

I think it's not the best approach for this issue. 我认为这不是解决此问题的最佳方法。 Actually, you can consider about a flag variable like "isSwitching". 实际上,您可以考虑使用诸如“ isSwitching”之类的标志变量。

rlFlipCamera.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (!isSwitching) {
            switchCamera();
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM