如何将onclick处理程序传递给自定义对话框类

Joh*_*ard 2 android dialog

我正在尝试创建一个自定义类以显示Yes / No AlertDialog,但我希望onClick处理程序位于实例化该自定义类的活动中。到目前为止,自定义类如下所示:

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();
  }
}
Run Code Online (Sandbox Code Playgroud)

我的想法是将上下文和onClick处理程序传递给构造函数中的对象,然后将处理程序分配给.setPositive和.setNegative按钮。

我在MainActivity类中实现了DialogInterface.OnClickListener:

public class MainActivity
        extends AppCompatActivity
        implements DialogInterface.OnClickListener {
Run Code Online (Sandbox Code Playgroud)

并在MainActivity中创建了onClick处理程序,当在对话框中单击“是”或“否”按钮时应调用该处理程序。

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

我不确定是否在正确的轨道上,但是我被困在试图弄清楚如何将onClick处理程序传递给YesNoDialog对象。我已经尝试了几种变体:

YesNoDialog dialog = new YesNoDialog(this, MainActivity.onClick);
Run Code Online (Sandbox Code Playgroud)

没有成功(不会编译)。我还尝试过仅传递上下文,假设也许这正是我真正需要的.setPositive和.setNegative按钮处理程序,但这也不起作用...此调用需要DialogInterface.OnClickListener。

感觉就像我接近了,但是我无法克服障碍。谁能帮我把点子联系起来

小智 6

创建一个类(DialogUtils)并在其中添加此方法。

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();
}
Run Code Online (Sandbox Code Playgroud)

像这样调用方法:

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"
    }
});
Run Code Online (Sandbox Code Playgroud)