简体   繁体   English

从单独的Dialog类将Dialog的单击侦听器获取到我的Activity中

[英]Get Dialog's on click listener into my Activity from separate Dialog class

I have a Dialog class where I have kept my dialogs. 我有一个Dialog类,其中保存了对话框。 Now the problem is that I want to get the View click listeners of my dialog back in my activity. 现在的问题是,我想在活动中重新获取对话框的“查看”单击侦听器。 I know this can be done by writing an interface but is there any other OOP way of doing it? 我知道可以通过编写接口来完成此操作,但是还有其他OOP方式可以做到吗?

My Dialog class: 我的Dialog类:

public class Dialogs{
 public void testCompletionDialog() {

        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.test_complete_dialog);
        dialog.setTitle("Ratta provet?");



        dialog.findViewById(R.id.lesson_btn_marker).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//I want my activity to know that this view is clicked.
                 dialog.dismiss();


            }
        });

        dialog.findViewById(R.id.lesson_btn_ratta).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //I want my activity to know that this view is clicked.


            }
        });

        dialog.show();


    }
}

My Activity: 我的活动:

if (areQueOver) {

                    Dialogs dialogs=new Dialogs(TestActivity.this);
                    dialogs.testCompletionDialog();
                }

You may use it using EventBus 您可以通过EventBus使用它

Inside your onClick in your Dialog class post an event telling that a dialog has been clicked. 在Dialog类的onClick内部发布一个事件,告诉您已单击一个对话框。 The event may contain a string variable telling which dialog is clicked. 该事件可能包含一个字符串变量,告诉您单击了哪个对话框。

Inside your Activity subscribe to and handle the event. 在您的活动中订阅并处理事件。 You may check the String variable value to know which dialog was clicked. 您可以检查String变量值以了解单击了哪个对话框。

Modify your Dialogs class as below: 如下修改对话框类:

public class Dialogs{
 public void testCompletionDialog() {

        final Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.test_complete_dialog);
        dialog.setTitle("Ratta provet?");

        dialog.findViewById(R.id.lesson_btn_marker).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 EventBus.getDefault().post("btn_marker");
                 dialog.dismiss();
            }
        });

        dialog.findViewById(R.id.lesson_btn_ratta).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post("btn_ratta");
            }
        });

        dialog.show();

Inside your Activity: 在您的活动中:

@Subscribe(threadMode = ThreadMode.MAIN) public void onEvent(String action){
        if(action.equals("btn_ratta")){

        } else if(action.equals("btn_marker")) {

        }
    }

inside onCreate add this- 在onCreate内添加此-

EventBus.getDefault().register(this);

inside onDestroy add this- 在onDestroy内添加此-

EventBus.getDefault().unregister(this);

Alternative method: 替代方法:

Well, other than interface and EventBus, you may add a public method to your Activity say, 好吧,除了接口和EventBus,您还可以在Activity中添加一个公共方法,例如:

onDialogClicked(String dialogName){//TODO handle the click as per dialogName} 

and then call this method from your onClick in your Dialogs class. 然后从Dialogs类的onClick调用此方法。

use listner for call buttons like this 使用列表器来像这样的通话按钮

Simpledialoginterface listner = new Simpledialoginterface() {
    @Override
    public void ok_button() {
        //ok button click
    }

    @Override
    public void cancel_button() {
       //cancel button click
    }
};

use this dialog 使用此对话框

public static void popupnew(String tittle, String message, String Button, String Cancel,
                            final Activity context, final Simpledialoginterface listner) {

    if (!((Activity) context).isFinishing()) {
        android.app.AlertDialog.Builder alertDialog;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            alertDialog = new android.app.AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
        } else {
            alertDialog = new android.app.AlertDialog.Builder(context);
        }
        alertDialog.setTitle(tittle);
        alertDialog.setMessage(message);
        alertDialog.setPositiveButton(Button,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        listner.ok_button();


                    }
                });
        alertDialog.setNegativeButton(Cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        listner.cancel_button();

                    }
                });
        alertDialog.show();
    }

}

//interface class public interface Simpledialoginterface { // interface类public interface Simpledialoginterface {

public void ok_button();

public void cancel_button();

} }

    popupnew("title","message","OK","Cancel",this,listner);//call dialog

Create an interface. 创建一个接口。

public interface OnDialogConfirmClickListener {
    void onDialogConfirmClick(Class parameter//or empty);
}

Implement this interface to your activity. 为您的活动实现此接口。

public class MainActivity extends Activity implements OnDialogConfirmClickListener {
...
}

Send interface as parameter to Dialogs or testCompletionDialog method. 将接口作为参数发送给Dialogs或testCompletionDialog方法。

public void testCompletionDialog(OnDialogConfirmClickListener listener) {
    ...
    dialog.findViewById(R.id.lesson_btn_marker).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     listener.onDialogConfirmClick(parameter//or empty);
                     dialog.dismiss();
                }
            });
    ...
    }

Yes if you want to call any method of Actvity then you can call through context of Activity : 是的,如果您想调用Actvity的任何方法,则可以通过Activity的上下文进行调用:

suppose method1() is under Activity and you want to call from Dailog then you can call through . 假设method1()在Activity下,并且您想从Dailog调用,则可以通过调用。

 ((MyActivity)((Activity)context)).method1();

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

相关问题 我的对话框出了什么问题?如何更改不同类和单独的.java文件中的arraylist? - What's wrong with my Dialog box and how should I change arraylist from different class and on separate .java file? 如何从单独类的对话框中返回值 - How to return a value from dialog in separate class 从侦听器创建对话框 - create dialog from listener 从对话框获取父活动的textview的值 - get the value of a textview of the parent activity from dialog 从单独的类中的侦听器禁用JButton - Disable a JButton from a listener that's in a separate class 在Dialog之类的Activity中保留回调侦听器是否安全? - Is it safe to keep a callback listener in Activity like Dialog? ClassCastException - 从对话片段的适配器类获取活动的视图引用 - ClassCastException - getting Activity's view reference from adapter class for dialog fragment 如何通过对话框类中的button-ActionPerformed从我的面板类中调用一个方法,该方法是它在面板类中的实例? - How to call a method from my panel class through the button-actionPerformed in the dialog-class, that get´s it´s instance in the panel-class? 在单击的侦听器中编辑警报对话框 - Editing an alert dialog inside an on-click listener 如何在与事件Vaadin分开的类中建立点击侦听器 - How to establish a click listener in a separate class from the event Vaadin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM