我正在尝试显示带有图像按钮的对话框,onClick图像第二个对话框应该用数据时间选择器打开,用户将选择应该在第一个对话框中反映的日期。但问题是点击图片,它没有用datetimepicker打开第二个对话框。
view.findViewById(R.id.action_bar_button_city).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// addCity(view);
LayoutInflater li = LayoutInflater.from(ViewTripActivity.this);
View promptsView = li.inflate(R.layout.city_prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ViewTripActivity.this);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
ib1 = (ImageButton)promptsView.findViewById(R.id.imageButton1);
ib2 = (ImageButton)promptsView.findViewById(R.id.imageButton2);
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
final EditText et3=(EditText)promptsView.findViewById(R.id.etarrivaldate);
final EditText et4=(EditText)promptsView.findViewById(R.id.etdeparturedate);
et3.setEnabled(false);
et3.setFocusable(false);
et4.setEnabled(false);
et4.setFocusable(false);
ib1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(1);
}
// @Override
@Deprecated
protected Dialog onCreateDialog(int id) {
return new DatePickerDialog(ViewTripActivity.this, datePickerListener1, year, month, day);
}
private DatePickerDialog.OnDateSetListener datePickerListener1 = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
Log.d("In ADD CITY","DAY"+selectedDay);
Log.d("In ADD CITY","month"+selectedMonth);
Log.d("In ADD CITY","year"+selectedYear);
et3.setText(selectedDay+"/" + (selectedMonth + 1)+"/"+ selectedYear);
}
};
});
ib2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(ViewTripActivity.this, "click of departuredate", Toast.LENGTH_SHORT).show();
showDialog(2);
}
@Deprecated
protected Dialog onCreateDialog(int id) {
return new DatePickerDialog(ViewTripActivity.this, datePickerListener2, year, month, day);
}
private DatePickerDialog.OnDateSetListener datePickerListener2 = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
Log.d("In ADD CITY","DAY"+selectedDay);
Log.d("In ADD CITY","month"+selectedMonth);
Log.d("In ADD CITY","year"+selectedYear);
et4.setText(selectedDay+"/" + (selectedMonth + 1)+"/"+ selectedYear);
}
};
});
//code to show alert window to add city
alertDialogBuilder.setCancelable(false).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Toast.makeText(ViewTripActivity.this, "You clicked on OK", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});