为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

开满天机 2023-02-23 16:53:45
我有一个 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());//solveddialog.dismiss}再次说明:看,我可以取消否定按钮内的对话框。但我想要的是,在那个对话框中,我膨胀了包含一个联系人列表的行。我希望当用户点击任何联系人时(比如说点击触摸监听器上的回收站)我正在使用单例传递一些数据,同时我想关闭对话框。
查看完整描述

1 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

这是通用目的的对话代码。您可以在需要显示对话框时随时调用。您可以通过调用方法显示对话框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;

   }

 }



查看完整回答
反对 回复 2023-02-23
  • 1 回答
  • 0 关注
  • 75 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信