取消/关闭来自同一类中其他任何方法的alertdialog构建器在android中? [英] Cancel/dismiss alertdialog builder from any other method in same class in android?

查看:92
本文介绍了取消/关闭来自同一类中其他任何方法的alertdialog构建器在android中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java类,没有活动.在任何其他活动中,我都调用了该类,并在该类中创建了alertdialog构建器.这样,我就可以从db扩充数据.

I have a java class not activity. From any other activity, I called this class and in that class I created alertdialog builder. In that I inflate data from db.

现在,在此类中,我还具有其他侦听器和方法.在一种方法中,我要关闭/取消此对话框.就像我们的方式

Now In this class, I have other listener and method also. In one of the method, I want to dismiss/cancel this dialog. Like how we do

setResult(RESULT_OK, intent);
        finish();

在任何活动中,我想在课堂上做同样的事情.

in any activity, same thing I want to do in class.

代码:我从活动中调用了此方法.

Code: This method I called from 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类中还有一个方法,我想从该方法中关闭currentlt打开的对话框.

And I have one more method in same java class, from that method I want to dismiss currentlt opened dialog.

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

再次说明:看,我可以取消否定按钮内的对话框.但是我想要的是,在该对话框中,我为包含一个联系人列表的行充气.我希望当用户单击任何联系人时(也就是单击触摸侦听器上的回收站)时,我正在使用单例传递一些数据,与此同时,我想关闭对话框.

Explain once again: See, I can cancel dialog inside negative button. But what I want is, in that dialog I inflate row which includes one contact list. I want that when user click on any of the contact (which is let's say clicked on recycler on touch listener) I'm passing some data using singleton and at that same time, I want to dismiss dialog.

推荐答案

此处是通用的对话框代码.您可以在需要显示对话框时随时呼叫.您可以通过调用showDialog()方法&来显示对话框.通过调用dismissDialog()方法关闭.

Here's dialog code for generic purpose. You can call whenever you need to show the dialog. You can show dialog by calling showDialog() method & dismiss by calling dismissDialog() method.

/*
* 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;
   }
 }

这篇关于取消/关闭来自同一类中其他任何方法的alertdialog构建器在android中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆