How do I reference the main activity's onClick

2019-09-05 14:09发布

问题:

I am trying to do something unusual. I have a main activity with a menu attached to the menu button. One item in the menu opens a dialog to pick a control that is added to the main activity. I have it working so it adds the control, and saves it in a database (so it will be remembered for the next run). I need to set the button's onClickListener to the main activity's onClick.

public class MyMainActivity extends Activity 
    implements View.OnClickListener, View.OnTouchListener
{

private Context mContext;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    AbsoluteLayout mMainActivityView = new AbsoluteLayout;
    SetContentView(mMainActivityView);
... populate mMainActivityView from database ...


public void onClick(View v) {
    switch (v.id) {
        case NEW_BUTTON_ID:
         // TODO implement click handler
    }
}
...

...
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case NEW_BUTTON_DIALOG_ID:
...
...    
            builder.setPositiveButton(android.R.string.ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            int XPos = Integer.valueOf(editXPos.getText().toString());
                            int YPos = Integer.valueOf(editYPos.getText().toString());
                            mDataLayer.AddControl(mScreenID, Width, Height, XPos, YPos, editButtonText.getText().toString());
                            Button button = new Button (mContext);
                            button.setLayoutParams(new AbsoluteLayout.LayoutParams(Width, Height, XPos, YPos));
                            button.setText(editButtonText.getText().toString());
                            mMainActivityView.addView(button);
                // How to set Listeners from main activity?
                            button.setOnClickListener(?????);
                            button.setOnTouchListener(?????);
                            MyMainActivity.this.removeDialog(NEW_BUTTON_DIALOG_ID);
                        }
                    });

            builder.setNegativeButton(android.R.string.cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    MyMainActivity.this.removeDialog(NEW_BUTTON_DIALOG_ID);
                    }
                });
        AlertDialog NewButtonDialog = builder.create();
        return NewButtonDialog;
    }
    return null;
}

So, how do I reverence the main activity's onClick() from withing the AlertDialog's button?

回答1:

Why do you need specific access to the onClick() method? Just create a public method in your main class and call it from both the dialog and the button's onClick() methods. Sadly DialogInterface and View don't share a superclass, but you can pass the Object and cast accordingly if necessary.



回答2:

As suggested by Selvin in the comments, I needed to set the onClickListener using: MyMainActivity.onClick