我正在尝试做一些不寻常的事情。我有一个附加到菜单按钮的菜单的主要活动。菜单中的一项会打开一个对话框以选择添加到主活动的控件。我让它工作,所以它添加了控件,并将其保存在数据库中(因此下次运行时会记住它)。我需要将按钮的 onClickListener 设置为主要活动的 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;
}
那么,如何onClick()
使用 AlertDialog 的按钮来尊重主要活动?