【问题标题】:AlertDialog not showing on Stepper Fragment步进器片段上未显示警报对话框
【发布时间】:2017-07-13 15:07:13
【问题描述】:

我正在使用 Android Material Stepper 库 (https://github.com/stepstone-tech/android-material-stepper) 实现一个步进器。在第三步,我正在实现一个 BlockingStep 以在 UI 被阻止/显示一个对话框时进行 Http 调用。

问题是对话框没有出现。我尝试使用 ProgressDialog 和 AlertDialog,但它们都没有出现在屏幕上。

@Override
public void onNextClicked(final StepperLayout.OnNextClickedCallback callback) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(R.layout.dialog_loader);
    builder.setCancelable(false);
    final AlertDialog dialog = builder.show();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            try {
                //Generating JSONObject

                //Using AsyncTask to end JSON
                TCPConfigurationTask task = new TCPConfigurationTask();
                String[] params = {mIp, mPort, configObject.toString(), "true"};
                String response = task.execute(params).get();

                //Consuming response
                if (response != null) {
                    Log.d("JSON_CODE_RESPONSE", response);
                }
            } catch (JSONException | InterruptedException | ExecutionException e) {
                jsonError = true;
            }
            dialog.dismiss();
            callback.goToNextStep();
        }
    }, 0L);
}

编辑:

答案中提供的代码很好。问题是 MainThread 在等待 AsyncTask 响应时做的工作太多。

【问题讨论】:

    标签: android android-alertdialog progressdialog


    【解决方案1】:

    替换这一行,

    final AlertDialog dialog = builder.show();
    

    AlertDialog alert = builder.create();
    alert.show();
    

    使用下方关闭对话框,

    alert.dismiss();
    

    使用这个,在你的 AlertDialog 中设置自定义布局,

    builder.setView(R.layout.dialog_loader);
    

    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout. dialog_loader, null);
    builder.setView(dialogView);
    

    【讨论】:

    • 正是我的想法
    • 我需要将 AlertDialog 保持为最终状态,以便在线程完成后将其关闭。
    • 是的,我就是这么说的,你可以使用dismiss来关闭线程中的alertdialog
    • 知道了。试过了,但它还没有工作。线程完成,但没有显示对话框。
    • 没有再次工作,但您的代码不是问题:由于String response = task.execute(params).get(); 行,MainThread 做了太多工作。我的初始代码可以正常工作,就像你的一样!如果您添加我的注释,我会接受您的回答,以便人们可以跟踪问题!
    猜你喜欢
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 2021-03-08
    • 2011-06-17
    • 1970-01-01
    相关资源
    最近更新 更多