[英]How to prevent double-click on refresh button
I'm developing an android app where i've a refresh button in my action bar. 我正在开发一个Android应用程序,其中的操作栏中有一个刷新按钮。 This button call a function that re-open the same activity. 此按钮调用重新打开相同活动的函数。 This activity contains an asyncTask to load the content. 此活动包含一个asyncTask来加载内容。
At the moment i'm encountering this problem. 目前,我遇到了这个问题。 When i click on the refresh button it works fine, but if i click on the refresh button when the AsyncTask is still working (i've a progress bar to check the status) the app crashes. 当我单击刷新按钮时,它工作正常,但是如果我在AsyncTask仍在运行时单击刷新按钮(我有一个进度条来检查状态),则应用程序将崩溃。
The error that i receive is: NullPointerException 我收到的错误是:NullPointerException
Is it possible to disable that button until the activity (and its AsyncTask) are completely loaded? 是否可以禁用该按钮,直到活动(及其AsyncTask)完全加载?
In your button's OnClickListener
, from where you execute the AsyncTask
, add this code: 在按钮的OnClickListener
,从中执行AsyncTask
,添加以下代码:
button.setEnabled(false);
In onPostExecute()
method of your AsyncTask
, place this: 在您的AsyncTask
onPostExecute()
方法中,将其放置:
button.setEnabled(true);
If you also give the 'cancel' option to the user(ie if you have overridden the onCancelled()
method in your AsyncTask
), enable the button in onCancelled()
. 如果您还向用户提供“取消”选项(即,如果您已覆盖AsyncTask
的onCancelled()
方法),请启用onCancelled()
的按钮。
Edit 1 : 编辑1 :
Declare a boolean
flag in your activity: 在活动中声明一个boolean
标志:
boolean menuButtonIsEnabled = true;
In your OnClickListener
, set this flag to false: 在您的OnClickListener
,将此标志设置为false:
menuButtonIsEnabled = false;
In onPostExecute()
method of your AsyncTask
: 在您的AsyncTask
onPostExecute()
方法中:
menuButtonIsEnabled = true;
Override the onPrepareOptionsMenu(Menu)
method in your activity: 在您的活动中覆盖onPrepareOptionsMenu(Menu)
方法:
@Override
public boolean onPrepareOptionsMenu (Menu menu){
super.onPrepareOptionsMenu(menu);
MenuItem button = menu.findItem(R.id.whatever_menu_button);
if(menuButtonIsEnabled){
button.setEnabled(true);
} else {
button.setEnabled(false);
}
return true;
}
In your onClickListener, the first thing you do is deactivate the button. 在您的onClickListener中,您要做的第一件事就是停用按钮。 This way it cannot be clicked again until you reactivate it : 这样,除非您重新激活它,否则无法再次单击它:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View target) {
target.setClickable(false);
target.setEnbled(false);
// Start your asynctask
}
}
in your AsyncTask.onPostResult(), you can reactivate the click on the button. 在您的AsyncTask.onPostResult()中,您可以重新激活按钮的单击。
Nota: setClickable(false) prevents the button from reacting to clicks events, but setEnabled(false) also usually changes the appearance of the button. 注意:setClickable(false)可以防止按钮对单击事件做出反应,但是setEnabled(false)通常还会更改按钮的外观。
您可以尝试从AsyncTask
onPreExecute
禁用按钮,并从AsyncTask
onPostExecute
启用按钮
You can check the status of your task.... On click of refresh button... 您可以检查任务的状态。...单击刷新按钮时...
if(yourAsyncTaskObject != null && yourAsyncTaskObject.getStatus() != AsyncTask.Status.RUNNING){
then start your activity again here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.