【问题标题】:How to close an activity from a custom dialog Class?如何从自定义对话框类中关闭活动?
【发布时间】:2011-08-04 23:35:47
【问题描述】:

我有一个AlertDialogUtils 类,它生成一个AlertDialog,以便在发生错误时可以从任何活动中调用它。问题是我不能从createDialog() 方法中调用finish() 作为“关闭”按钮的onClickListener

你有什么想法吗?

AlertDialogUtils 类的代码:

public class AlertDialogUtils extends Dialog {

    private Context mContext;

    public AlertDialogUtils(Context context) {
        super(context);
        mContext = context;
    }

    public void CreateAlertDialog(String errorMessage) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(errorMessage)
            .setCancelable(true)
            .setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    mContext.finish(); 
                    //error here. Intend to close the activtiy that created this dialog and has the error
                }
            });
        AlertDialog alert = builder.create();
        alert.setOwnerActivity((Activity)mContext); 
        // The dialog utils is outside an activity. Need to set owner
        alert.show();
    }
}

【问题讨论】:

  • 也许这不是最直接的方法,但你为什么不把Activity作为参数传递给CreateAlertDialog(String)呢?

标签: android class android-activity dialog


【解决方案1】:

请你试试这个方法好吗? :

interface ICloseActivity {
void close();
}

class MyActivityToClose extends Activity implements ICloseActivity {

public void close() {
finish();
}

}

// -------

public class AlertDialogUtils extends Dialog {

    private Context mContext;

    private ICloseActivity mICloseActivity;

    public AlertDialogUtils(Context context, ICloseActivity aActivity) {
        super(context);
        mContext = context;
        mICloseActivity = aActivity;
    }

    public void CreateAlertDialog(String errorMessage) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setMessage(errorMessage)
           .setCancelable(true)
           .setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
               //    mContext.finish(); 
              //error here. Intend to close the activtiy that created this dialog and has the error

                //TRY THIS please:
                mICloseActivity.close();
               }
           });
    AlertDialog alert = builder.create();
    alert.setOwnerActivity((Activity)mContext); 
          // The dialog utils is outside an activity. Need to set owner
    alert.show();
}

【讨论】:

    【解决方案2】:

    不要在构造函数中传递Context,而是传递Activity。它继承了Context,所以你可以在任何需要Context的地方使用它;同时,您也可以在需要时拨打finish()

    【讨论】:

      【解决方案3】:

      感谢您的建议。 ICloseActivity,不幸的是,对我不起作用。我通过以下方式解决了完成活动并关闭对话框的问题: 我将CreateAlertDialog()方法改成如下,并创建了一个辅助函数exitActivity()

      public void CreateAlertDialog(String title, String errorMessage, int alertType){
          AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                   builder.setTitle(title)  
              .setIcon(R.drawable.alert_icon)
              .setMessage(errorMessage)
              .setCancelable(false)
              .setPositiveButton("Dismiss", new AlertDialog.OnClickListener() {
                             public void onClick(DialogInterface dialog, int id) {
                                 dismiss = true;
                                 exitActivity();
      
                             }
                         });
              AlertDialog alert = builder.create();
              alert.setOwnerActivity((Activity)mContext);
                  alert.show();
                      }
                   public void exitActivity() {
                this.getOwnerActivity().finish();
                }
      

      在调用此实用程序时,我只需设置关闭侦听器并完成活动以防它被关闭。

                   dialog = new AlertDialogUtils(MyActivity.this);
          dialog.setOwnerActivity(MyActivity.this);
          dialog.CreateAlertDialog(getString(R.string.no_data),       
                              getString(R.string.empty_table_message), R.id.list_view_alertType);
          dialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
              @Override
              public void onDismiss(DialogInterface arg0) {
                  finish();
              }
          });
      

      通过这种方式,如果需要,我可以处理关闭当前活动。 此外,对于我不想关闭而不是调用exitActivity() 的活动,我调用了dialog.cancel()

                     .setPositiveButton("Dismiss", new AlertDialog.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                      cancel();
      
                   }
              });
      

      【讨论】:

        猜你喜欢
        • 2017-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多