Is it possible to simplify the Runnable command in an Android function?

0

I've created a class to make it easier to use Dialogs, and I'd like to know if you can simplify it even further.

My Class looks like this:

public class FVRDialog {
    private Activity act;
    private Context context;
    private AlertDialog dialog;

    public FVRDialog(Activity act) {
        this.act = act;
    }

    public boolean Confirm(int icon, String Title, String ConfirmText,
                       String OkBtn, String CancelBtn, final Runnable OkBtnPress, final Runnable CancelBtnPress) {

    dialog = new AlertDialog.Builder(act).create();
    dialog.setTitle(Title);
    dialog.setMessage(ConfirmText);
    dialog.setCancelable(false);
    if (icon != 0) { dialog.setIcon(icon); }
    dialog.setButton(DialogInterface.BUTTON_POSITIVE, OkBtn,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int buttonId) {
                    OkBtnPress.run();
                }
            });
    dialog.setButton(DialogInterface.BUTTON_NEGATIVE, CancelBtn,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int buttonId) {
                    CancelBtnPress.run();
                }
            });
    dialog.show();
    return true;
   }

    public void dismiss() {
        dialog.dismiss();
    }
}

And to execute I'm doing so:

final FVRDialogs fvrdialog = new FVRDialogs(this);
       fvrdialog.Confirm(@drawable/ic_pergunta, "Titulo", "Descrição", "Sim","Não",
           new Runnable() { public void run() { executarBtnSim(); } },
           new Runnable() { public void run() { executarBtnNao(); } });

I would like to simplify without having to use runnable, leave it as possible, more or less like this:

FVRDialogs fvrdialog = new FVRDialogs(this);
 fvrdialog.Confirm(@drawable/ic_pergunta, "Titulo", "Descrição", "Sim","Não", executarBtnSim(), executarBtnNao());

Or if you have some other interesting way to do this, I'm open to suggestions.

    
asked by anonymous 18.04.2018 / 08:42

1 answer

3

Android Studio 3 supports some Java 8 features . You can enable this by adding the following lines in build.grad:

android {
  ...
  compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
  }
}

This way you can pass a lambda as a parameter:

fvrdialog.confirm(@drawable/ic_pergunta, "Titulo", "Descrição", 
    "Sim","Não", () -> executarBtnSim(), () -> executarBtnNao());

Or a method reference:

fvrdialog.confirm(@drawable/ic_pergunta, "Titulo", "Descrição", 
    "Sim","Não", this::executarBtnSim, this::executarBtnNao);
    
18.04.2018 / 12:27