0

我正在尝试调用自定义对话框中的按钮单击事件。这是我的自定义对话框代码

protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
        case MY_DIALOG:
            dialog= getInstanceMyDialog();
            break;
        default:
            dialog = null;
    }
    return dialog;
}

private Dialog getInstanceMyDialog() {
    final Dialog bookMarkDialog= new Dialog(this,R.style.BookmarkDialogNoTitle);
    bookMarkDialog.setContentView(R.layout.bookmark_dialog);
    return bookMarkDialog;
}

我的对话框添加和取消中有两个按钮。我不知道在哪里触发按钮单击事件。

请帮我。提前致谢

4

2 回答 2

1
private Dialog getInstanceMyDialog() {
  final Dialog bookMarkDialog= new Dialog(this,R.style.BookmarkDialogNoTitle);
  bookMarkDialog.setContentView(R.layout.bookmark_dialog);

  Button addButton = (Button)bookMarkDialog.findViewById(R.id.dialogAddButton);
  addButton.setOnClickListener(new OnClick .........)
  ......... same for cancel.

  return bookMarkDialog;
}

没有测试它,但应该可以工作:)

于 2012-09-18T08:19:09.787 回答
0

您可以放大视图并设置按钮的侦听器。

private Dialog getInstanceMyDialog() {
    final Dialog bookMarkDialog= new Dialog(this,R.style.BookmarkDialogNoTitle);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View contentView = inflater.inflate(R.layout.bookmark_dialog, root);
    /// set the listener for your buttons.
    Button button1 = (Button) contentView.findViewById(R.id.button1);
    button1.setOnItemClickListener(this);

    bookMarkDialog.setContentView(contentView);
    return bookMarkDialog;
}
于 2012-09-18T08:18:48.670 回答