在Java for Android中点击外部自定义对话框时,OnDissmissListener和OnCancelListener未执行

时间:2014-07-09 18:35:35

标签: java android dialog

我有一个自定义对话框,当用户退出时,主要活动将通过使用启动活动进行刷新。到目前为止,情况并非如此,因为在没有任何一个侦听器执行的情况下关闭对话框。按下后退按钮以及按下屏幕上的对话框时会发生这种情况。

public class CustomDialogClass extends Dialog implements
    android.view.View.OnClickListener {

public Activity mActivity; 

public CustomDialogClass(Activity activity) {
    super(activity);
    this.mActivity = activity;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.custom_dialog);

    setCanceledOnTouchOutside(true);

    new OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            // TODO Auto-generated method stub
            Log.e("check", "STARTED_dismiss");
            Intent i = new Intent(mActivity, MainActivity.class);
            mActivity.startActivity(i);

        }
    };

    new OnCancelListener(){

        @Override
        public void onCancel(DialogInterface dialog) {
            // TODO Auto-generated method stub
            Log.e("check", "STARTED_Cancel");
            Intent i = new Intent(mActivity, MainActivity.class);
            mActivity.startActivity(i);
        }

    };
}

2 个答案:

答案 0 :(得分:0)

您必须设置侦听器,以便在onCreate方法中调用 setOnDismissListener setOnCancelListener 方法。像这样:

setOnDismissListener(new OnDismissListener() {
   @Override
   public void onDismiss(DialogInterface dialog) {
       // your code here
   }
});

setOnCancelListener(new OnCancelListener() {
   @Override
   public void onCancel(DialogInterface dialog) {
       // your code here
   }
});

答案 1 :(得分:0)

您创建匿名侦听器。你需要处理你的倾听者。 做这样的事情:

OnDismissListener onDismissListener = new OnDismissListener() {

    @Override
    public void onDismiss(DialogInterface dialog) {
        // TODO Auto-generated method stub
        Log.e("check", "STARTED_dismiss");
        Intent i = new Intent(mActivity, MainActivity.class);
        mActivity.startActivity(i);

    }
};

OnCancelListener onCancelListener = new OnCancelListener(){

    @Override
    public void onCancel(DialogInterface dialog) {
        // TODO Auto-generated method stub
        Log.e("check", "STARTED_Cancel");
        Intent i = new Intent(mActivity, MainActivity.class);
        mActivity.startActivity(i);
    }

};

然后将侦听器添加到您的对象:

myObject.setOnDismissListener(onDismissListener);
myObject.setOnCancelListener(onCancelListener);