我有一个 timePicker 对话框,可以从不同的按钮调用。如何传递调用对话框的 ID?
这是相关的代码:
//Time picker
private int pHour;
private int pMinute;
static final int TIME_DIALOG_ID = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/** Get the current time */
final Calendar cal = Calendar.getInstance();
pHour = cal.get(Calendar.HOUR_OF_DAY);
pMinute = cal.get(Calendar.MINUTE);
}
按钮的点击事件:
public void onClick(View v) {
// Here I should pass the v.getId() to the dialog
showDialog(TIME_DIALOG_ID);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, pHour, pMinute, false);
}
return null;
}
/** Callback received when the user "picks" a time in the dialog */
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
pHour = hourOfDay;
pMinute = minute;
//Here I should know from which v.getId() the dialog was called
}
};
谢谢!