AlertDialog未在Stepper Fragment上显示

时间:2017-07-13 15:07:13

标签: android alertdialog progressdialog

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

问题是Dialog没有显示出来。我尝试使用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响应。

1 个答案:

答案 0 :(得分:3)

替换此行,

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