Multiple OK / Cancel dialogs, how to say in onClick (), in which dialog box?

I created the general class OkCancelDialog, which conveniently runs throughout the application through a static method:

static public void Prompt(String title, String message) { OkCancelDialog okcancelDialog = new OkCancelDialog(); okcancelDialog.showAlert(title, message); } 

For various reasons, I need an onClick listener in activity, so in activity I have:

  public void onClick(DialogInterface v, int buttonId) { if (buttonId == DialogInterface.BUTTON_POSITIVE) { // OK button // do the OK thing } else if (buttonId == DialogInterface.BUTTON_NEGATIVE) { // CANCEL button // do the Cancel thing } else { // should never happen } } 

This works fine with one dialog box in the application, but now I want to add another OK / Cancel dialog handled by the same action. As far as I can tell, only one onClick() can be defined for activity, so I'm not sure how to implement this.

Any suggestions or tips?

+4
source share
2 answers

Try something like this ...

 public class MyActivity extends Activity implements DialogInterface.OnClickListener { // Declare dialogs as Activity members AlertDialog dialogX = null; AlertDialog dialogY = null; ... @Override public void onClick(DialogInterface dialog, int which) { if (dialogX != null) { if (dialogX.equals(dialog)) { // Process result for dialogX } } if (dialogY != null) { if (dialogY.equals(dialog)) { // Process result for dialogY } } } } 

EDIT This is how I create my AlertDialogs ...

 private void createGuideViewChooserDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Select Guide View") .setSingleChoiceItems(guideViewChooserDialogItems, currentGuideView, this) .setPositiveButton("OK", this) .setNegativeButton("Cancel", this); guideViewChooserDialog = builder.create(); guideViewChooserDialog.show(); } 
+5
source

You have two options. 1) You can set OnClickListener in the dialog itself. 2) You can specify in which dialog box the DialogInterface dialog box will be displayed.

+1
source

Source: https://habr.com/ru/post/1413246/


All Articles