0

我有这个自定义对话框类

public class CustomDialog extends Dialog implements android.view.View.OnClickListener{

      public Activity c;
      public Dialog d;
      public Button cancelButton;
      public LinearLayout smsButton, emailButton;

      public CustomDialog(Activity a) {
        super(a);
        // TODO Auto-generated constructor stub
        this.c = a;
      }

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);
        smsButton = (LinearLayout)findViewById(R.id.smsButton);
        emailButton = (LinearLayout)findViewById(R.id.emailButton);
        cancelButton = (Button)findViewById(R.id.cancelButton);

      }

      @Override
      public void onClick(View v) {
        switch (v.getId()) {
        case R.id.smsButton:
            Toast.makeText(getContext(), "SMS", Toast.LENGTH_SHORT).show();
            break;
        case R.id.emailButton:
            Toast.makeText(getContext(), "EMAIL", Toast.LENGTH_SHORT).show();
            break;
        case R.id.cancelButton:
            dismiss();
            break;
        default:
        break;
        }
        dismiss();
      }

}

在我的主要活动中,我将其实现为:

btn = (Button)findViewById(R.id.reportVAW);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                CustomDialog cd = new CustomDialog(MainActivity.this);
                cd.show();

            }
        });

一切似乎都运行良好,但onClickListeners按钮似乎不起作用。在 代码中看到的自定义对话框类中:

@Override
      public void onClick(View v) {
        switch (v.getId()) {
        case R.id.smsButton:
            Toast.makeText(getContext(), "SMS", Toast.LENGTH_SHORT).show();
            break;
        case R.id.emailButton:
            Toast.makeText(getContext(), "EMAIL", Toast.LENGTH_SHORT).show();
            break;
        case R.id.cancelButton:
            dismiss();
            break;
        default:
        break;
        }
        dismiss();
      }

所有这些根本不起作用,知道为什么吗?

4

1 回答 1

1

您永远不会将 onClickListener 设置为按钮。这就是缺少的:

onCreate(Bundle savedInstanceState) {
    // your existing code
    smsButton.setOnClickListener(this);
    emailButton.setOnClickListener(this);
    cancelButton.setOnClickListener(this);
}
于 2013-09-02T03:21:16.250 回答