参考button not shown in alertDialog
中的上一个问题我正在创建一个扩展AlertDialog的类。在类中,我从xml设置了具有按钮的内容,但我的按钮没有响应。我的自定义警报java文件是
public class DateTimeDialog extends AlertDialog{
Date date;
String title;
View.OnClickListener listner;
protected DateTimeDialog(Context context, String title, Date date ) {
super(context, android.R.style.Theme_Holo_Light_Dialog);
// TODO Auto-generated constructor stub
this.title = title;
this.date = date;
}
public void initListener(View.OnClickListener listner){
this.listner = listner;
}
@Override
public void onCreate(Bundle savedInstanceState){
//super.onCreate(savedInstanceState);
setContentView(R.layout.date_time_picker);
setTitle(title);
Button dialogButtonOK = (Button) findViewById(R.id.btn_ok);
// if button is clicked, close the custom dialog
dialogButtonOK.setOnClickListener(listner);
Button dialogButtonCancel = (Button) findViewById(R.id.btn_cancel);
// if button is clicked, close the custom dialog
dialogButtonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View dialog) {
// TODO Auto-generated method stub
}
});
}
调用此类的方法是
final DateTimeDialog dateTimeDialog = new DateTimeDialog(context, "Title", date);
dateTimeDialog.show();
dateTimeDialog.initListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//done something
}
});
问题是点击时没有调用我的ok按钮但是调用了cancel。 我不知道我哪里出错了。请帮忙!!!!
答案 0 :(得分:0)
试试这个:
public void initListener(View.OnClickListener listner){
this.listner = listner;
Button dialogButtonOK = (Button) findViewById(R.id.btn_ok);
dialogButtonOK.setOnClickListener(listner);
}