【发布时间】: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