简体   繁体   English

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

[英]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. 我正在尝试创建一个用于显示Yes / No AlertDialog的自定义类,但我希望onClick处理程序位于实例化自定义类的活动中。 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. 我的想法是将上下文和onClick处理程序传递给构造函数中的对象,然后将处理程序分配给.setPositive和.setNegative按钮。

I implemented the DialogInterface.OnClickListener in my MainActivity class: 我在MainActivity类中实现了DialogInterface.OnClickListener:

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. 并在MainActivity中创建了onClick处理程序,当在对话框中单击“是”或“否”按钮时,应该调用该处理程序。

@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. 我不确定我是否在正确的轨道上,但是我一直试图找出如何将onClick处理程序传递给YesNoDialog对象。 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. 我也尝试过只传递上下文,假设这可能是我真正需要的.setPositive和.setNegative按钮处理程序,但这也不起作用......这个调用需要一个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. 创建一个类(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();
}

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. 这在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. 为对话框创建DialogFragment ,以便在设备旋转时正确恢复或以其他方式更改配置。
  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. 将活动DialogFragment onAttachDialogFragmentonAttach内的接口,并将其存储在某个字段中。 Don't forget to set to null in onDetach . 不要忘记在onDetach设置为null
  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. 嘿,你可以在MainActivity类中创建一个方法。 Like below. 如下。

public void onClickOnYesButton(int id){

}

Pass the MainActivity reference like below. 传递MainActivity参考,如下所示。

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

And call the onClickOnYessButton by using the MainActivity reference! 并使用MainActivity引用调用onClickOnYessButton

Job done! 任务完成!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在对话框的Button的onClick处理程序中关闭自定义对话框,并读取对话框元素的值 - Dismiss a custom dialog in the onClick handler of the dialog's Button and read dialog element's value 如何使用FragmentActivity在XML中为对话框按钮定义onClick处理程序方法? - How to define onClick handler method in XML for dialog buttons using FragmentActivity? 自定义对话框onClick错误 - Custom Dialog onClick error 如何将自定义参数传递给onClick侦听器 - How to pass custom parameters into onClick listener 如何在Android中完成后将onclick事件传递给方法以关闭对话框 - How to pass a onclick event into a method to dismiss dialog after completion in Android 如何在自定义 Dialog 类中设置接口和侦听器以将 Facebook 的登录回调传递给 MainActivity 中的 firebase auth? - How to set up Interface and listener in custom Dialog class to pass Facebook's login callback to firebase auth in MainActivity? 如何在Android中的活动中自定义对话框中单击视图? - How to get onclick of a view in custom dialog in an activity in android? 外部类中的Android对话框处理程序 - Android Dialog Handler in external class 如何关闭处理程序中的对话框 - how to dismiss dialog in handler 如何通过意图传递自定义类 - How to pass custom class in an intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM