Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a listener to handle onBackPressed event directly from Dialog #320

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions MaterialDesign/src/com/gc/materialdesign/widgets/Dialog.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.gc.materialdesign.widgets;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
Expand All @@ -15,7 +17,7 @@
import com.gc.materialdesign.R;
import com.gc.materialdesign.views.ButtonFlat;

public class Dialog extends android.app.Dialog{
public class Dialog extends android.app.Dialog implements DialogInterface.OnKeyListener {

Context context;
View view;
Expand All @@ -32,6 +34,7 @@ public class Dialog extends android.app.Dialog{

View.OnClickListener onAcceptButtonClickListener;
View.OnClickListener onCancelButtonClickListener;
OnKeyDownListener onKeyDownListener;


public Dialog(Context context,String title, String message) {
Expand All @@ -49,6 +52,12 @@ public void addCancelButton(String buttonCancelText, View.OnClickListener onCanc
this.buttonCancelText = buttonCancelText;
this.onCancelButtonClickListener = onCancelButtonClickListener;
}

public void setOnKeyDownListener(OnKeyDownListener onKeyDownListener) {
if (onKeyDownListener != null) {
this.onKeyDownListener = onKeyDownListener;
}
}


@Override
Expand Down Expand Up @@ -84,8 +93,8 @@ public boolean onTouch(View v, MotionEvent event) {
@Override
public void onClick(View v) {
dismiss();
if(onAcceptButtonClickListener != null)
onAcceptButtonClickListener.onClick(v);
if (onAcceptButtonClickListener != null)
onAcceptButtonClickListener.onClick(v);
}
});

Expand All @@ -103,11 +112,27 @@ public void onClick(View v) {
}
});
}

setOnKeyListener(this);
}


@Override
public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(keyEvent.getAction() != KeyEvent.ACTION_DOWN) {
dismiss();
if (onKeyDownListener != null) {
onKeyDownListener.onBackPressed();
}
return true;
}
}

return false;
}

@Override
public void show() {
// TODO 自动生成的方法存根
super.show();
// set dialog enter animations
view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.dialog_main_show_amination));
Expand Down Expand Up @@ -215,6 +240,7 @@ public void run() {
backView.startAnimation(backAnim);
}



public interface OnKeyDownListener {
void onBackPressed();
}
}