android - 在 AsyncTask 期间禁用按钮

标签 android button android-asynctask

我编写了一个测验应用程序,如果我在 AsyncTask 中触摸其中一个答案(如果答案正确),我将颜色设置为绿色,如果错误则设置为红色。

但现在在 AsyncTask 运行期间,我可以按其他按钮,例如“更改问题”按钮或另一个答案。然后在 AsyncTask 完成其工作后完成。所以下一个问题被加载,它会自动回答下一个问题或使用任何一个 clown 。

我尝试设置启用(假)按钮,但它们仍然在窃听。

如何防止这种情况?

private void disableOrDisableButtons(boolean boo) {
        buttonAnswer1.setEnabled(boo);
        buttonAnswer2.setEnabled(boo);
        buttonAnswer3.setEnabled(boo);
        buttonAnswer4.setEnabled(boo);
    }

然后我在这里启动 AsyncTask

disableOrDisableButtons(false);
new PrepareAdapter().execute(null, null, null);

在我的异步任务中

@Override
        protected void onPreExecute() {
            disableOrDisableButtons(false);
            if (correctAnswerAtButton != buttonClicked) {
                switch (buttonClicked) {
                case 1:
                    buttonAnswer1.setTextColor(Color.RED);
                    break;
                case 2:
                    buttonAnswer2.setTextColor(Color.RED);
                    break;
                case 3:
                    buttonAnswer3.setTextColor(Color.RED);
                    break;
                case 4:
                    buttonAnswer4.setTextColor(Color.RED);
                    break;
                }
                if (buttonClicked != 0) { // 0.. if second chance joker used
                    wrongAnswer = true;
                }
            }
            switch (correctAnswerAtButton) {
            case 1:
                buttonAnswer1.setTextColor(Color.GREEN);
                return;
            case 2:
                buttonAnswer2.setTextColor(Color.GREEN);
                return;
            case 3:
                buttonAnswer3.setTextColor(Color.GREEN);
                return;
            case 4:
                buttonAnswer4.setTextColor(Color.GREEN);
                return;
            }
        }

最佳答案

如果你想在 AsyncTask 运行时禁用整个界面,你可以使用如下代码显示一个对话框:

public abstract class BaseAsyncTask<Param, Result> extends AsyncTask<Param, Void, Result> implements DialogInterface.OnCancelListener {
    private static final String TAG = "BaseAsyncTask";
    private ProgressDialog dialog = null;
    protected Context ctx = null;
    protected Exception exception = null;

    public BaseAsyncTask(Context ctx) {
        this.ctx = ctx;
    }

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(ctx, WLConstants.MSG_TITLE_LOADING_DIALOG, WLConstants.MSG_LOADING_DIALOG, true);
        dialog.setCancelable(true);
        dialog.setOnCancelListener(this);
        if (ctx instanceof WozzonActivity) {
            ((WozzonActivity) ctx).setCurrentDialog(dialog);
        }
    }

    @Override
    protected Result doInBackground(Param... parameters) {
        try {
            return inBackground(parameters);
        } catch (Exception ex) {
            exception = ex;
            Log.e(TAG, ex.getClass().getName(), ex);
            return null;
        }
    };

    @Override
    protected void onPostExecute(Result result) {
        try {
            dialog.dismiss();
        } catch (Exception ex) {
        }// TODO:
        if (result == null) {
            onException(exception);
        } else {
            onResult(result);
        }
    }

    protected void onException(Exception ex) {
        if (ex != null && ex instanceof WozzonException) {
            Toast.makeText(ctx, ex.getMessage(), Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(ctx, WLConstants._ERROR_MSG, Toast.LENGTH_SHORT).show();
        }

    }

    public abstract void onResult(Result result);
    public abstract Result inBackground(Param... parameters) throws Exception;

    @Override
    public void onCancel(DialogInterface theDialog) {
        cancel(true);
    }
}

关于android - 在 AsyncTask 期间禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9378910/

相关文章:

javascript - <button> 点击后总是重定向

javascript - jQuery - 单击按钮时增加计数器的值

java - 从 asynctask 返回变量到 UI

android - 某些设备的 facebook 错误 key 散列无效

android - 如何用动画扩展布局高度?

android - 错误 : java. lang.NoClassDefFoundError : android. support.v4.content.LocalBroadcastManager

java - 如何异步读取 JSON 并在 android 中使用其数据?

android - 数据库在我的应用程序下载后似乎消失了。想法?

android - 修复了即使滑动 viewPager 也会保留的按钮

java - SplashScreen 中的 AsyncTask