所以在我的应用程序的一个页面上,有两个 EditText 字段(editEarliestDepart 和 editLatestDepart)。
我希望发生两件事:
当用户单击其中一个时,我希望出现一个 TimePicker 片段对话框。用户设置时间后,这些值将被传递并显示在刚刚选择的 EditText 字段中。
当用户设置“editLatestDepart”时,确保“editLatestDepart”总是在“editEarliestDepart”之后。
谁能帮我解决这个问题?我已经尝试了几天,这让我发疯。
谢谢,
凯文
调用 Timepicker 片段对话框的 Activity。
public class NewDriverSetupTime extends FragmentActivity implements TimePickedListener {
String pickerHour, pickerMinute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_driver_setup_time);
//create on click listener for earliest depart
EditText earliestDepart = (EditText) findViewById(R.id.editEarliestDepart);
earliestDepart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerDialogFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
});
//create on click listener for latest depart
EditText latestDepart = (EditText) findViewById(R.id.editLatestDepart);
latestDepart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment newFragment = new TimePickerDialogFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
});
}
@Override
public void onTimePicked(Calendar time) {
// TODO Auto-generated method stub
// display the selected time in the clicked editview
}
}
这是实际的片段代码
public class TimePickerDialogFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
private TimePickedListener mListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onAttach(Activity activity) {
// when the fragment is initially shown (i.e. attached to the activity), cast the activity to the callback interface type
super.onAttach(activity);
try
{
mListener = (TimePickedListener) activity;
}
catch (ClassCastException e)
{
throw new ClassCastException(activity.toString() + " must implement " + TimePickedListener.class.getName());
}
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// when the time is selected, send it to the activity via its callback interface method
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hourOfDay);
c.set(Calendar.MINUTE, minute);
mListener.onTimePicked(c);
}
public static interface TimePickedListener
{
public void onTimePicked(Calendar time);
}
}