如何在对话框的onClick方法中引用主要活动的onClick ...还是可以? [英] How do I reference the main activity's onClick from within the onClick method of a dialog... or can I?

查看:81
本文介绍了如何在对话框的onClick方法中引用主要活动的onClick ...还是可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些不寻常的事情.我的主要活动是将菜单附加到菜单按钮.菜单中的一项将打开一个对话框,以选择一个添加到主活动中的控件.我正在使用它,因此它添加了控件,并将其保存在数据库中(以便下次运行时会记住该控件).我需要将按钮的onClickListener设置为主活动的onClick.

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

那么,如何使用AlertDialog的按钮来尊重主要活动的onClick()?

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

推荐答案

正如Selvin在评论中所建议的,我需要使用以下内容来设置onClickListener:MyMainActivity.onClick

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

这篇关于如何在对话框的onClick方法中引用主要活动的onClick ...还是可以?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆