好吧,我觉得我明白了。我只是不喜欢这个解决方案,因为我在一周中使用的Spinner与timepicker的“主题”不匹配,所以看起来非常糟糕。
编辑:我改变了微调的布局,你可以在帖子底部看到它,现在看起来好多了。
DayTimePickerFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View thisDialog = inflater.inflate(R.layout.picker_day_time, container, false);
Button btnDateTimeOK = (Button)thisDialog.findViewById(R.id.btnDateTimeOK);
Button btnDateTimeCancel = (Button)thisDialog.findViewById(R.id.btnDateTimeCancel);
final Spinner spinSelectedDay = (Spinner)thisDialog.findViewById(R.id.spinSelectedDay);
ArrayAdapter adapter = new ArrayAdapter(getActivity(), R.layout.spinner_item_day_picker, getResources().getStringArray(R.array.days_of_week));
spinSelectedDay.setAdapter(adapter);
final TimePicker tpSelectedTime = (TimePicker)thisDialog.findViewById(R.id.tpSelectedTime);
btnDateTimeCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ActionBarActivity thisActivity = (ActionBarActivity) getActivity();
getDialog().dismiss();
}
});
btnDateTimeOK.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ActionBarActivity thisActivity = (ActionBarActivity) getActivity();
String strTime = null, strDay = null, strHour = null, strMinute = null, strAM_PM = null;
strDay = ((String)spinSelectedDay.getSelectedItem());
strMinute = tpSelectedTime.getCurrentMinute().toString();
if (tpSelectedTime.getCurrentMinute() < 10){
strMinute = "0"+strMinute;
}
if (tpSelectedTime.getCurrentHour() > 12){
strHour = (tpSelectedTime.getCurrentHour() - 12)+"";
strAM_PM = "PM";
}else if(tpSelectedTime.getCurrentHour() == 12){
strHour = tpSelectedTime.getCurrentHour().toString();
strAM_PM = "PM";
}else if(tpSelectedTime.getCurrentHour() < 12){
strHour = tpSelectedTime.getCurrentHour().toString();
strAM_PM = "AM";
}
if (strHour != null && strAM_PM != null){
strTime = strHour + ":" + strMinute + " " + strAM_PM;
}
etTarget.setText(strDay + " " + strTime);
getDialog().dismiss();
}
});
return thisDialog;
}
picker_day_time.xml
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
android:layout_width="wrap_content" android:layout_height="wrap_content" >
android:layout_height="wrap_content" >
android:text="@string/button_text_cancel" android:onClick="DateTimeClickCancel" />
android:layout_weight="1" android:text="@string/button_text_ok" android:onClick="DateTimeClickOK" />
spinner_item_day_picker.xml
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="@dimen/text_size_x_large" android:padding="10dp" android:gravity="center"/>