【问题标题】:Cancel/dismiss alertdialog builder from any other method in same class in android?从android中同一类中的任何其他方法取消/关闭警报对话框构建器?
【发布时间】:2019-06-29 09:30:12
【问题描述】:

我有一个 java 类而不是活动。在任何其他活动中,我调用了这个类,并在该类中创建了 alertdialog builder。在那我膨胀了来自 db 的数据。

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

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
}

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

【问题讨论】:

  • 你能把整个代码贴在这里吗?何时关闭对话框?
  • @SanjayBhalani 这几乎是完整的代码,其他的东西不是必须的,否则读者会混淆。看,我可以取消否定按钮内的对话框。但我想要的是,在那个对话框中,我增加了包含一个联系人列表的行。我希望当用户单击任何联系人(让我们说触摸监听器上的回收器)时,我正在使用单例传递一些数据,同时我想关闭对话框。
  • 我可以发布我的通用对话框代码吗?
  • @SanjayBhalani 当然,但你明白我的问题吗?
  • 是的,我明白你想要什么

标签: java android android-dialog


【解决方案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;
   }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-08
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    相关资源
    最近更新 更多