这是Java代码:
public class ReservationFragment extends DialogFragment implements AdapterView.OnItemSelectedListener, View.OnClickListener {
private ReservationFragment.OnFragmentInteractionListener mListener;
private EditText fromDateEtxt;
private EditText fromTimeEtxt;
private DatePickerDialog fromDatePickerDialog;
private TimePickerDialog fromTimePickerDialog;
private SimpleDateFormat dateFormatter;
private SimpleDateFormat timeFormatter;
public ReservationFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_reservation, container, false);
/** Date & Time Picker **/
dateFormatter = new SimpleDateFormat("dd.MM.yyyy", Locale.US);
fromDateEtxt = (EditText) v.findViewById(R.id.reservation_etxt6);
fromDateEtxt.setInputType(InputType.TYPE_NULL);
fromDateEtxt.requestFocus();
timeFormatter = new SimpleDateFormat("hh:mm", Locale.US);
fromTimeEtxt = (EditText) v.findViewById(R.id.reservation_etxt7);
fromTimeEtxt.setInputType(InputType.TYPE_NULL);
fromTimeEtxt.requestFocus();
setDateField();
setTimeField();
return v;
}
private void setDateField() {
fromDateEtxt.setOnClickListener(this);
java.util.Calendar newCalendar = java.util.Calendar.getInstance();
fromDatePickerDialog = new DatePickerDialog(getActivity(), R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
java.util.Calendar newDate = java.util.Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
Toast.makeText(getActivity(), "You choose: " + dayOfMonth + "." + (monthOfYear + 1) + "." + year, Toast.LENGTH_LONG).show();
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(java.util.Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
private void setTimeField(){
fromTimeEtxt.setOnClickListener(this);
java.util.Calendar newCalendar = java.util.Calendar.getInstance();
fromTimePickerDialog = new TimePickerDialog(getActivity(), R.style.DialogTheme, new TimePickerDialog.OnTimeSetListener(){
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
java.util.Calendar newTime = java.util.Calendar.getInstance();
newTime.set(hourOfDay, minute);
fromTimeEtxt.setText(timeFormatter.format(newTime.getTime()));
Toast.makeText(getActivity(), "You choose: " + hourOfDay + ":" + minute, Toast.LENGTH_LONG).show();
}
},newCalendar.get(java.util.Calendar.HOUR_OF_DAY), newCalendar.get(java.util.Calendar.MINUTE), false);
}
@Override
public void onClick(View v) {
if(v == fromDateEtxt) {
fromDatePickerDialog.show();
}
else if(v == fromTimeEtxt) {
fromTimePickerDialog.show();
}
}
请帮帮我。一切正常,但我只想将选定的时间放在EditText NOT CURRENT TIME中。
但是我必须告诉我Toast工作正常,它显示选定的时间,但EditText没有。
答案 0 :(得分:0)
您将错误的值传递给Calendar.set(int, int)
。看看the documentation。
它应该是这样的:
java.util.Calendar newTime = java.util.Calendar.getInstance();
newTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
newTime.set(Calendar.MINUTE, minute);
fromTimeEtxt.setText(timeFormatter.format(newTime.getTime())