I have a DialogInterface.OnClickListener interface connected to a class inherited from DialogFragment.

As I understand it, this interface requires you to implement these methods. The last two are for what I understand.

@Override public void onClick(DialogInterface dialog, int which) { } @Override public void onDismiss(DialogInterface unused) { super.onDismiss(unused); } @Override public void onCancel(DialogInterface unused) { super.onCancel(unused); } 

Judging by the documentation, the first is needed to determine which button is pressed and what to do. If so, then how can I use it so that I can specify which code should be executed when I click on the button, which is in the markup that is connected to this dialog fragment. First I will find this button

  Button vk_share = (Button)form.findViewById(R.id.vk_button); 

What's next?

    1 answer 1

    The documentation says:

    clicked button (ex. BUTTON_POSITIVE) or clicked on

    Once you are inherited, is this some kind of custom dialog? It is unlikely that this interface will receive an ID from a custom button. Just find the button and ask her the right listener.
    UPD Yes, I understood you, but you connected the custom button through the markup, though I don’t know if it can be said that way: D
    The bottom line is that in the usual dialog box there are three buttons - positive, negative and neutral. Well, something like OK - CANCEL ... You can specify them:

     public class FireMissilesDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the Builder class for convenient dialog construction AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.dialog_fire_missiles) .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // FIRE ZE MISSILES! } }) .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { // User cancelled the dialog } }); // Create the AlertDialog object and return it return builder.create(); } 

    And instead of specifying the listeners immediately for such buttons, you can implement the interface that you wrote. And then the onClick method in the variable which will receive one of the three constants - BUTTON_NEGATIVE, BUTTON_NEUTRAL or BUTTON_POSITIVE. Further, I think it is clear. Related links - https://developer.android.com/guide/topics/ui/dialogs.html , https://developer.android.com/reference/android/content/DialogInterface.html#BUTTON_POSITIVE

    • not. The window is not custom. After all, the dialog fragment can display what I set in xml, i.e. the window i create is what i want. And I added a button to it, not a custom one. So I want to put a listener on it. I will find the button. I think that you can connect the View.OnClickListener interface, but I am still interested in what the first method in the code I cited is needed - Turalllb
    • Well, I thought that this is due to these three custom buttons. I wanted confirmation. And I myself have already implemented a different interface, View.OnClickListener, and in it, Id find the button and everything works. Only something that does not please me: I cannot write dialog.dismiss () in this method like this; Those. so that my dialogue after I click on the button first executed my code and then closed. It is necessary to organize the flow of dialogue in this method. Here in DialogInterface. OnClickListener comes the dialogue. - Turalllb
    • one
      Hmm, I think there should be something like getDialog (). But this is not accurate, as a last resort - you can save the reference to the dialog when you call onCreate - R. Bes
    • getDialog (). dismiss (); Yes it is) Works as it should. thanks - Turalllb