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

萝らか妹 提交于 2020-05-31 03:57:48

问题


I have a Dialog class where I have kept my dialogs. 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?

My Dialog class:

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();
                }

回答1:


Yes if you want to call any method of Actvity then you can call through context of Activity :

suppose method1() is under Activity and you want to call from Dailog then you can call through .

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



回答2:


You may use it using EventBus

Inside your onClick in your Dialog class post an event telling that a dialog has been clicked. 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.

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-

EventBus.getDefault().register(this);

inside onDestroy add this-

EventBus.getDefault().unregister(this);

Alternative method:

Well, other than interface and EventBus, you may add a public method to your Activity say,

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

and then call this method from your onClick in your Dialogs class.




回答3:


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 {

public void ok_button();

public void cancel_button();

}

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



回答4:


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.

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();
                }
            });
    ...
    }


来源:https://stackoverflow.com/questions/42572674/get-dialogs-on-click-listener-into-my-activity-from-separate-dialog-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!