我尝试使用此代码来防止在ImageVIEw中多次单击,但这无济于事.
Boolean isClicked = false; @OverrIDe public voID onClick(VIEw v) { if (v == imgClick && !isClicked) { //lock the image isClicked = true; Log.d(TAG, "button click"); try { //I try to do some thing and then release the image vIEw Thread.sleep(2000); } catch (InterruptedException e) { e.printstacktrace(); } isClicked = false; } }
在日志猫中,当我尽快单击ImageVIEw 5次时,可以看到5行“按钮单击”.我可以看到log cat打印第一行,等待一会儿(2秒),然后打印下一行.我想当我单击ImageVIEw时,触发的事件将按顺序移到队列中,不是吗?那么我该如何阻止呢?
我也尝试使用setEnable()或setClickable()代替isClicked变量,但是它也不起作用.
解决方法:
只需尝试此工作代码
Boolean canClick = true; //make global variable Handler myHandler = new Handler();@OverrIDepublic voID onClick(VIEw v){ if (canClick) { canClick= false; //lock the image myHandler.postDelayed(mMyRunnable, 2000); //perform your action here }}/* give some delay..*/ private Runnable mMyRunnable = new Runnable() { @OverrIDe public voID run() { canClick = true; myHandler.removeMessages(0); } };Instead of sleePing in 2 seconds, I use some task like doSomeThing() method (has accessed UI thread), and I don't kNow when it completed. So how can I try your way?
//我提到了这个androIDlink.您可以更有效地处理线程,但是我希望下面的代码对您有用.
//您尝试这个
Boolean canClick = true; //make global variablepublic voID onClick(VIEw v) { if(canClick){ new DownloadImageTask().execute(); } } private class DownloadImageTask extends AsyncTask<String, VoID, Bitmap> { protected Bitmap doInBackground(String... urls) { Log.d("MSG","Clicked"); canClick =false; //perform your long operation here return null; } protected voID onPostExecute(Bitmap result) { canClick =true; } }
总结 以上是内存溢出为你收集整理的Java-避免在图像视图android中多次单击全部内容,希望文章能够帮你解决Java-避免在图像视图android中多次单击所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)