我有这个自定义对话框类:
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();
}
所有这些根本不起作用,知道为什么吗?