简体   繁体   English

防止小部件在短时间内多次被点击

[英]Prevent a widget from getting clicked multiple times in a short span

I have a button that when clicked, saves a canvas to an image file and then calls another activity. 我有一个按钮,单击该按钮会将画布保存到图像文件,然后调用另一个活动。 The problem I am having is that through testing, I've noticed the user can click the button twice in a row before the activity is called, resulting in two files being written. 我遇到的问题是,通过测试,我注意到用户可以在调用活动之前连续两次单击按钮,从而导致写入两个文件。

I also noticed the same problem with spinners in another activity. 我还注意到另一个活动中的微调框存在相同的问题。 I click them twice in a row in succession and up pops up 2 list views containing all my spinner items. 我连续两次单击它们,然后弹出弹出包含我所有微调器项目的2个列表视图。

I've been trying to Google this without much luck. 我一直在尝试使用Google,但运气不佳。 Is there a simple fix to this? 有一个简单的解决办法吗?

Thanks. 谢谢。

You can have a simple boolean value like mClickBeingProcessed that you set to true as soon as your method that saves the canvas is called. 您可以拥有一个简单的布尔值,例如mClickBeingProcessed,您将在调用保存画布的方法后立即将其设置为true。 If it's true, you don't do anything, otherwise you save the canvas and call the other activity. 如果是真的,则您什么都不做,否则保存画布并调用其他活动。 At the end of your method (or other processing) set it back to false. 在方法(或其他处理)结束时,将其设置回false。

Another option is to store a timestamp for the most recent click. 另一种选择是存储最近点击的时间戳。 If the click happens within X time of the previous click (whatever value you decide, maybe 1000ms), it is ignored. 如果点击发生在上一次点击的X时间内(无论您决定的是什么值,可能是1000ms),它将被忽略。

simply show a progress dialog till your processing is going on 只需显示一个进度对话框,直到您的处理正在进行
see this. 看到这个。
http://developer.android.com/guide/topics/ui/dialogs.html http://developer.android.com/guide/topics/ui/dialogs.html
thanks. 谢谢。

Well as it turns out, I had to implement both suggestions to get this to work. 事实证明,我必须实施两个建议才能使它起作用。 +1 to each of you. +1给你们每个人。

I used an indeterminate progress dialog, and a boolean value like so: 我使用了一个不确定的进度对话框和一个布尔值,如下所示:

    Button send = (Button) findViewById(R.id.send);
    send.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View v) {
            if (mAllowSave) {
                mAllowSave = false;
                showDialog(SAVING_DIALOG);
                mSaveFileThread = new SaveFileThread(handler);
                mSaveFileThread.start();
            }
        }
    });

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

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