Cancel/dismiss 来自 android 中相同 class 任何其他方法的警报对话框生成器?

Cancel/dismiss alertdialog builder from any other method in same class in android?

我有 java class 而不是 activity。在任何其他 activity 中,我将此命名为 class,并在那个 class 中创建了 alertdialog 生成器。在那里我膨胀了来自数据库的数据。

现在在这个 class 中,我还有其他侦听器和方法。在其中一种方法中,我想 dismiss/cancel 这个对话框。喜欢我们的做法

setResult(RESULT_OK, intent);
        finish();

在任何 activity 中,我想在 class 中做同样的事情。

代码:我从 activity.

调用的这个方法
 public void showProvidersDialog(long customCategoryId) {
        categoryId = customCategoryId;
        LayoutInflater li = LayoutInflater.from(context);
        promptsView = li.inflate(R.layout.row_providers_layout, null);
        init();
        alertDialogBuilder = new android.app.AlertDialog.Builder(context, R.style.dialogBoxStyle);
        alertDialogBuilder.setView(promptsView);

        alertDialogBuilder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        isInsurance();
        alertDialogBuilder.show();
//solved:  dialog = alertDialogBuilder.create();
        dialog.show();
        }

我还有一个相同的方法 java class,我想通过该方法关闭当前打开的对话框。

  private void sendProviderData(General provider) {
        Singleton.getInstance().setProviderId(provider.getId());
        Singleton.getInstance().setProviderIcon(provider.getIcon());
        Singleton.getInstance().setProviderName(provider.getName());
//solved
dialog.dismiss
}

再次说明:看,我可以在否定按钮内取消对话框。但我想要的是,在那个对话框中,我膨胀了包含一个联系人列表的行。我希望当用户点击任何联系人时(比如说点击触摸监听器上的回收站)我正在使用单例传递一些数据,同时我想关闭对话框。

这是通用的对话框代码。您可以在需要显示对话框时随时调用。您可以通过调用 showDialog() 方法显示对话框并通过调用 dismissDialog() 方法关闭。

/*
* call whenever dialog is required in whole app in form of popup
*/
public class MyDialog implements View.OnClickListener {
  private Dialog dialog;
  private Context context;
  private TextView tvTitle;
  private TextView tvSubtitle;
  private Button bt_ok;
  private String strInvalidUserNamePass, strHeader;
  /*
    * constructor to change the text dynamically.
  */
  public MyDialog(Context context, String strHeader, String invalidUserNamePass) {
     this.context = context;
     this.strInvalidUserNamePass = invalidUserNamePass;
     this.strHeader = strHeader;
     if (context != null) {
         initDialog();
     }
 }
 /*
  * set id of all the view components and implement listeners
  */
 private void initDialog() {

    dialog = new Dialog(context, R.style.FMDialogNormal);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_validation);
    dialog.setCancelable(false);
    dialog.setCanceledOnTouchOutside(false);
    dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    dialog.show();

    tvTitle = (TextView) dialog.findViewById(R.id.tv_title);
    tvSubtitle = (TextView) dialog.findViewById(R.id.tv_subtitle);
    tvTitle.setText(strHeader);
    tvSubtitle.setText(strInvalidUserNamePass);
    bt_ok = (Button) dialog.findViewById(R.id.bt_ok);
    bt_ok.setOnClickListener(this);

}
/*
 * Implement listener according to the views
 */
 @Override
 public void onClick(View view) {
     switch (view.getId()) {
         case R.id.bt_ok:
             dialog.dismiss();
             break;
     }
 }
 public void showDialog(){
     if(dialog!=null){
         dialog.show();
     }
 }
 public void dismissDialog(){
     if(dialog!=null && isVisible()){
         dialog.show();
     }
 }  
 public boolean isVisible() {
     if (dialog != null) {
         return dialog.isShowing();
     }
     return false;
   }
 }