【发布时间】:2014-01-29 10:18:41
【问题描述】:
我有一个布局,其中放置了一个 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 时,我得到的是当前系统时间而不是选定时间。
如何获得选定的时间?
【问题讨论】:
标签: android timepicker