Prevent a widget from getting clicked multiple times in a short span

痴心易碎 提交于 2019-12-12 02:39:58

问题


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.

I've been trying to Google this without much luck. Is there a simple fix to this?

Thanks.


回答1:


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. 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.

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.




回答2:


simply show a progress dialog till your processing is going on
see this.
http://developer.android.com/guide/topics/ui/dialogs.html
thanks.




回答3:


Well as it turns out, I had to implement both suggestions to get this to work. +1 to each of you.

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();
            }
        }
    });


来源:https://stackoverflow.com/questions/4653963/prevent-a-widget-from-getting-clicked-multiple-times-in-a-short-span

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!