从 android 中同一类中的任何其他方法取消/关闭 alertdialog 生成器?

我有一个 java 类没有活动。在任何其他活动中,我都调用了此类,并在该类中创建了 alertdialog 构建器。在那里我膨胀了来自数据库的数据。


现在在这个类中,我还有其他侦听器和方法。在其中一种方法中,我想关闭/取消此对话框。就像我们怎么做


setResult(RESULT_OK, intent);

        finish();

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


代码:我从活动中调用的这个方法。


 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 类中还有一个方法,我想从那个方法中关闭当前打开的对话框。


  private void sendProviderData(General provider) {

        Singleton.getInstance().setProviderId(provider.getId());

        Singleton.getInstance().setProviderIcon(provider.getIcon());

        Singleton.getInstance().setProviderName(provider.getName());

//solved

dialog.dismiss

}

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


开满天机
浏览 76回答 1
1回答

摇曳的蔷薇

这是通用目的的对话代码。您可以在需要显示对话框时随时调用。您可以通过调用方法显示对话框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;   } }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java