我有自定义对话框类:
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();
}
所有这些根本不起作用,任何想法为什么?
答案 0 :(得分:1)
您永远不会将onClickListener设置为按钮。 那就是缺少的东西:
onCreate(Bundle savedInstanceState) {
// your existing code
smsButton.setOnClickListener(this);
emailButton.setOnClickListener(this);
cancelButton.setOnClickListener(this);
}