在Android項目我有這個類的Java TimePicker不返回正確的值
package mycalories.com.jalle.mycalories;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.widget.TimePicker;
import java.util.Calendar;
public class TimePickerFragment extends DialogFragment implements
TimePickerDialog.OnTimeSetListener {
public static interface OnTimeSetListener {
void onTimeSet(Time time);
}
private OnTimeSetListener onTimeSetListener;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
if (onTimeSetListener != null) {
Time newTime =new Time(hourOfDay+":"+minute+ "");
onTimeSetListener.onTimeSet(newTime);
}
}
public void setOnTimeSetListener(OnTimeSetListener onTimeSetListener) {
this.onTimeSetListener = onTimeSetListener;
}
}
應該返回選定時間。而在主要活動我有按鈕和文本字段:
Button btnTime = (Button) findViewById(R.id.btnTime);
btnTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TimePickerFragment timePicker = new TimePickerFragment();
timePicker.show(getFragmentManager(), "timePicker");
timePicker.setOnTimeSetListener(new OnTimeSetListener() {
@Override
public void onTimeSet(Time time) {
// SimpleDateFormat HHMM = new SimpleDateFormat("hh:mm");
final EditText txtTime = (EditText) findViewById(R.id.txtTime);
txtTime.setText(time.toString());
}
});
}
});
選擇時間後,我應該在hh:mm格式,而是我19700101T00000011:55(4,0,0,0,0) (顯然當前時間沒有選擇,但默認的一個) 我想方法onTimeSet
是不正確的。任何想法 ?
還有一個問題:如何在diff活動中爲多個文本框使用相同的TimePicker類?