简体   繁体   中英

how to pass an onclick handler to a custom dialog class

I'm trying to create a custom class for displaying a Yes/No AlertDialog, but I want to onClick handler to be in the activity that instantiates the custom class. So far, the custom class looks like this:

public class YesNoDialog  {
private Context gContext = null;
private DialogInterface.OnClickListener onClickListener;
private AlertDialog alertDialog = null;

public YesNoDialog(Context context,
               DialogInterface.OnClickListener listener) {
    this.gContext = context;
    this.onClickListener = listener;
}

public void ShowDialog() {

    AlertDialog.Builder alertDialogBuilder = new  
                      AlertDialog.Builder(this.gContext);
    alertDialogBuilder.setTitle("Hello World");
    alertDialogBuilder
            .setMessage("Are you sure?")
            .setCancelable(false)
            .setPositiveButton("Yes",this.onClickListener)
            .setNegativeButton("No",this.onClickListener);
    alertDialog = alertDialogBuilder.create();
    alertDialog.show();
  }
}

My thinking was to pass the context and onClick handler to the object in the constructor, then assign the handler to the .setPositive and .setNegative buttons.

I implemented the DialogInterface.OnClickListener in my MainActivity class:

public class MainActivity
        extends AppCompatActivity
        implements DialogInterface.OnClickListener {

And created the onClick handler in MainActivity that should be called when either the Yes or No buttons are clicked in the dialog.

@Override
public void onClick(DialogInterface dialog, int id) {
    Log.d("DIALOG RETURNS ID=", Integer.toString(id));
    dialog.dismiss();
}

I'm not sure if I'm on the right track or not, but I got stuck in trying to figure out how I would now pass the onClick handler to the YesNoDialog object. I've tried several variations of this:

YesNoDialog dialog = new YesNoDialog(this, MainActivity.onClick);

With no success (won't compile). I have also tried passing only the context, assuming that maybe that's all I really need for .setPositive and .setNegative button handlers, but that didn't work either...this calls require a DialogInterface.OnClickListener.

It feels like I'm close, but I can't get over the hurdle. Can anyone help me connect the dots?

Create a class (DialogUtils) and add this method in it.

public static void showPopUp(Context context
            , String title
            , String msg
            , String positiveBtnTxt
            , String negativeBtnTxt
            , DialogInterface.OnClickListener positiveBtnListener
            , DialogInterface.OnClickListener negativeBtnListener){

    final AlertDialog errorDialog;
    AlertDialog.Builder errorDialogBuilder = new AlertDialog.Builder(context, R.style.NativeDialogue);
    errorDialogBuilder.setTitle(title);
    errorDialogBuilder.setMessage(msg);
    errorDialogBuilder.setPositiveButton(positiveBtnTxt, positiveBtnListener);
    errorDialogBuilder.setNegativeButton(negativeBtnTxt, negativeBtnListener);
    errorDialog = errorDialogBuilder.create();
    errorDialog.show();
}

Call the method like this :

DialogUtils.showPopUp(this, "title", "message", "positive btn name", "Negative Btn name", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        "Your action"
    }
}, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        "Your action"
    }
});

This is described in the official documentation on dialogs in Android. In short, you need to do the following steps:

  1. Create a DialogFragment for your dialog so it is properly restored when the device rotates or changes the configuration in some other way.
  2. Create an interface which will allow you to send the result of the dialog.
  3. Implement this interface in the activity.
  4. Cast the activity to the interface inside the DialogFragment in onAttach and store it in some field. Don't forget to set to null in onDetach .
  5. When a dialog button is clicked, you can call the appropriate interface method, and the activity will get the result.

Alternatively, if you only ever use this dialog with one activity, you may not declare an interface and simply store a reference to the activity.

Hey you can make one method in your MainActivity class. Like below.

public void onClickOnYesButton(int id){

}

Pass the MainActivity reference like below.

public YesNoDialog(MainActivity context) {
this.gContext = context;
}

And call the onClickOnYessButton by using the MainActivity reference!

Job done!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM