我有一个布局,我在其中放置了一个TimePicker和两个用于选择时间的按钮:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TimePicker android:id="@+id/time_picker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<Button android:id="@+id/time_picker_ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/yes"
android:onClick="set"/>
<Button android:id="@+id/time_picker_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/no"
android:onClick="cancel"/>
</LinearLayout>
</LinearLayout>
我通过其他活动的方法调用它:
public void showPicker(View view) {
LayoutInflater li = LayoutInflater.from(this);
View dialogView = li.inflate(R.layout.time_picker, null);
dialogView = new TimePicker(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.time_start_label);
builder.setView(dialogView);
ad = builder.create();
ad.show();
}
我有一个“是”按钮的处理程序:
public void set(View view) {
LayoutInflater li = LayoutInflater.from(this);
LinearLayout ll = (LinearLayout) li.inflate(R.layout.time_picker, null);
TimePicker tp = (TimePicker) ll.findViewById(R.id.time_picker);
int hour = tp.getCurrentHour();
int minute = tp.getCurrentMinute();
Utils.showMessage(this, hour + " " + minute); // Toast message
}
但是当我点击@ + id / time_picker_ok时,我得到的是当前的系统时间而不是选定的时间。
我如何获得选定的时间?
答案 0 :(得分:2)
getCurrentXXX()应该回答当前的小时或分钟。
要读取用户输入的时间,您必须使用如下监听器:
// the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
有关详细信息,请参阅本教程:http://developer.android.com/resources/tutorials/views/hello-timepicker.html
答案 1 :(得分:0)
警告:它使用 JodaTime的 DateTime
作为回报。您可以修改它以使用您喜欢的任何内容。
// Gets the Date and Time from a DatePicker and TimePicker and return a JodaTime DateTime from them, in current timezone.
//
private DateTime getDateTimeFromPickers( int DatePickerId, int TimePickerId ) {
DatePicker dp = (DatePicker) findViewById(DatePickerId);
TimePicker tp = (TimePicker) findViewById(TimePickerId);
String year = Integer.toString(dp.getYear()) ;
String month = StringUtils.leftPad( Integer.toString(dp.getMonth() + 1), 2, "0" );
String day = StringUtils.leftPad( Integer.toString(dp.getDayOfMonth()), 2, "0" );
String hour = StringUtils.leftPad( Integer.toString(tp.getCurrentHour()), 2, "0" );
String minutes = StringUtils.leftPad( Integer.toString(tp.getCurrentMinute()), 2, "0" );
String dateTime = year + "-" + month + "-" + day + "T" + hour + ":" + minutes + ":00.000";
return DateTime.parse(dateTime);
}
希望有所帮助。
JP