我使用本手册http://developer.android.com/guide/topics/ui/controls/pickers.html创建了 TimePicker
public class TimePickerFragment extends DialogFragment {
private TimePickerDialog.OnTimeSetListener mListener;
private Activity mActivity;
private Calendar mCalendar = Calendar.getInstance();
public TimePickerFragment(Activity a,TimePickerDialog.OnTimeSetListener listener) {
mListener = listener;
mActivity = a;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
int minute = mCalendar.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(mActivity, mListener, hour, minute,
DateFormat.is24HourFormat(mActivity));
}
}
我正在尝试以这种方式显示带有标签的此对话框
public class SomeActivity extends FragmentActivity implements TimePickerDialog.OnTimeSetListener {
TimePickerFragment mTimePicker;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_view);
mTimePicker = new TimePickerFragment(this, this);
// calling show with "SOME_TAG" tag
mTimePicker.show(this.getSupportFragmentManager(), "SOME_TAG");
}
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
// Here tag is null ?!
// Why?
String tag = (String) timePicker.getTag();
}
}
但在 onTimeSet 方法中,tag 始终为 null。为什么?