简体   繁体   English

如何以编程方式关闭 Android 材料警报对话框?

[英]How to close an Android Material Alert Dialog Programatically?

I have a dialog that appears whenever some background process is going on and displays a loading ProgressBar.每当一些后台进程正在进行时,我都会出现一个对话框,并显示一个正在加载的 ProgressBar。 I am however unable to dismiss it as the dismiss() function does not work.但是,我无法将其关闭,因为dismiss() function 不起作用。 In my code below in the hideLoading() function is supposed to close the dialog but .dismiss() doesn't work.在我下面的代码中, hideLoading() function 应该关闭对话框,但.dismiss()不起作用。 Kindly assist.请协助。

MainActivity.Java MainActivity.Java

public class LoginActivity extends AppCompatActivity {
MaterialAlertDialogBuilder progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    progress = new MaterialAlertDialogBuilder(LoginActivity.this);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (password_text.getText().length() < 5) {
                password_layout.setError(getResources().getString(R.string.valid_short_password));
            } else {
                showLoading();
            }
        }
    });
}

private void showLoading() {
    progress.setView(R.layout.loading);
    progress.setCancelable(false);
    progress.show();
}

private void hideLoading() {
    progress.dismiss();
}
}

Your progress object is an MaterialAlertDialogBuilder , not an AlertDialog itself.您的progress object 是MaterialAlertDialogBuilder ilder ,而不是AlertDialog本身。 You can't call dismiss on the builder.您不能在构建器上调用驳回。 First get an AlertDialog object from the builder via progress.create() , then show it.首先通过progress.create()从构建器获取AlertDialog ,然后显示它。 After that, you'll also be able to dismiss it.之后,您也可以将其关闭。 Eg例如

public class LoginActivity extends AppCompatActivity {
MaterialAlertDialogBuilder progress;
AlertDialog progressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    progress = new MaterialAlertDialogBuilder(LoginActivity.this);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (password_text.getText().length() < 5) {
                password_layout.setError(getResources().getString(R.string.valid_short_password));
            } else {
                showLoading();
            }
        }
    });
}

private void showLoading() {
    progress.setView(R.layout.loading);
    progress.setCancelable(false);
    progressDialog = progress.create();
    progressDialog.show();
}

private void hideLoading() {
    progressDialog.dismiss();
}
}

Note: I'm not sure how cancelable property works on the alert dialog, but you might also allow it to be cancelable to be able to dismiss it.注意:我不确定cancelable属性如何在警报对话框上工作,但您也可以允许它可以取消以便能够将其关闭。 If the above code doesn't work, try adding progress.setCancelable(true) .如果上面的代码不起作用,请尝试添加progress.setCancelable(true)

In your code, "progress" is not a dialog instance, it's a MaterialDialogBuider instance.在您的代码中,“进度”不是对话框实例,而是 MaterialDialogBuider 实例。 You should get an instance of AlertDialog by something like this.您应该通过这样的方式获得 AlertDialog 的实例。

MaterialAlertDialogBuilder builer = new MaterialAlertDialogBuilder(MainActivity.this);
builer.setView(R.layout.loading);
builer.setCancelable(false);
AlertDialog dialog = builder.show();

In order to dismiss the dialog, you can call dismiss function like this.为了关闭对话框,您可以像这样调用dismiss function。

dialog.dismiss();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM