0

我有一个自定义对话框类,如下所示,其中 xmlView = R.layout.yourdialoglayout 有 2 个按钮。如何向这些按钮添加侦听器?

这是我的课:

public class CustomDialog extends Dialog {
public CustomDialog(Context context,int theme,int xmlView) {
    super(context,theme);
    requestWindowFeature(Window.FEATURE_NO_TITLE); //Hide the title
    this.setContentView(xmlView);
    }

public void killDialog() {
    dismiss();
}

}

4

2 回答 2

1

您可以使用 View.SetOnClickListener 像附加 Activity 一样简单地附加OnClickListener

public CustomDialog(Context context, int theme, int xmlView)
{
    super(context,theme);
    requestWindowFeature(Window.FEATURE_NO_TITLE); // hide the title
    this.setContentView(xmlView);

    // your special button
    Button yourButton = findViewById(R.id.yourbutton);
    yourButton.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // your action
        }
    });
}

您可以以相同的方式将操作附加到其他按钮。

于 2011-08-22T10:06:23.327 回答
0

您可以使用findViewById找到按钮,并OnClickListener像往常一样设置它们

于 2011-08-22T04:16:03.797 回答