简体   繁体   中英

Handling external class dialog button in main activity

I am trying to create a custom dialog where the button clicks could be handled by the main activity that instantiates it, something like how the AlertDialog.Builder assigns the listeners via its setPositiveButton() and setNegativeButton() methods.

This is what I've done:

// THE MAIN ACTIVITY

public class main_code extends AppCompatActivity {

  private commonModule comMod;

  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      comMod = new commonModule(this);      
  }

  private void showDialog() {
    DialogInterface.OnClickListener dialogHandler = 
      new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        // process "PROCEED" button
      }
    };  

    commonModule.myDialog customDialog = comMod.new myDialog();
    customDialog.inputBox(this, "Submit Results", 
      "Your results will be submitted.", dialogHandler);
  }
}


// THE COMMON MODULE CLASS

public class commonModule {

  public commonModule(Context context){
    this.context = context;
    this.activity = (Activity) context;
  }

  public class myDialog {    

    public void showDialog(Activity activity, String title, String message,
                           DialogInterface.OnClickListener dialogHandler) {

      final Dialog panel = new Dialog(activity);
      panel.requestWindowFeature(Window.FEATURE_NO_TITLE);
      panel.setContentView(R.id.customLayout);
      panel.setCancelable(false);

      TextView panelTitle = (TextView) panel.findViewById(R.id.title);
      panelTitle.setTypeface(fontRes(PlayRegular));
      panelTitle.setText(title);

      TextView msgboxText = (TextView) panel.findViewById(R.id.content);
      msgboxText.setTypeface(fontRes(PlayRegular));
      msgboxText.setText(message);

      Button button1 = (Button) panel.findViewById(R.id.button1);
      button1.setTypeface(fontRes(PlayRegular));
      button1.setText("ABORT");
      button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          panel.dismiss();
        }
      });

      Button button2 = (Button) panel.findViewById(R.id.button2);
      button2.setTypeface(fontRes(PlayRegular));
      button2.setText("PROCEED");

      button2.setOnClickListener(dialogHandler);   
      // error: View.OnClickListener cannot be applied to Dialog.OnClickListener

      panel.show();
    }
  }
}

I tried using View.OnClickListener() as well, but to no success. I would like the dialog builder to be common and generic, and as such, the click listener should be unique for each dialog.

Any suggestion would be really appreciated.

TIA.

Change your dialogHandler to View.OnClickListener instead of DialogInterface.OnClickListener :

private void showDialog() {
    View.OnClickListener dialogHandler = 
      new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        // process "PROCEED" button
      }
    };  

    commonModule.myDialog customDialog = comMod.new myDialog();
    customDialog.inputBox(this, "Submit Results", 
      "Your results will be submitted.", dialogHandler);
  }

and in the common module:

public void showDialog(Activity activity, String title, String message,
                           View.OnClickListener dialogHandler) {
   ...
   button2.setOnClickListener(dialogHandler);
   panel.show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM