Android中的星期和时间选择器,用于重复发生的事件

时间:2013-12-23 15:37:28

标签: android android-timepicker

我想创建一个控件,允许用户在我的Android活动中选择一周中的某一天(星期一)和一天中的某个时间(下午1:00)。没能找到任何好的帖子?

3 个答案:

答案 0 :(得分:2)

好吧,我想我弄明白了。我只是不喜欢这个解决方案,因为我在星期几使用的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<String> adapter = new ArrayAdapter<String>(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

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <Spinner android:id="@+id/spinSelectedDay" android:layout_width="fill_parent" android:layout_height="wrap_content">
        </Spinner>

        <TimePicker android:id="@+id/tpSelectedTime"
            android:layout_width="wrap_content" android:layout_height="wrap_content" >
        </TimePicker>

        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <Button android:id="@+id/btnDateTimeCancel" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1"
                android:text="@string/button_text_cancel" android:onClick="DateTimeClickCancel" />

            <Button android:id="@+id/btnDateTimeOK" android:layout_width="0dip" android:layout_height="wrap_content"
                android:layout_weight="1" android:text="@string/button_text_ok" android:onClick="DateTimeClickOK" />
        </LinearLayout>

    </LinearLayout>

spinner_item_day_picker.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:text="Thursday"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:textSize="@dimen/text_size_x_large" android:padding="10dp" android:gravity="center"/>

答案 1 :(得分:2)

您可以尝试使用一些外部库,如android-betterpickers。至少在我看来,它有一个很棒的设计:

https://github.com/derekbrameyer/android-betterpickers

答案 2 :(得分:2)

我在一个对话框中结合了一周的时间和时间选择器。

您可以查看图书馆here

首先创建一个监听器:

final SlideDayTimeListener listener = new SlideDayTimeListener() {

    @Override
    public void onDayTimeSet(int day, int hour, int minute)
    {
        // Do something with the day, hour and minute
        // the user has selected.
    }

    @Override
    public void onDayTimeCancel()
    {
        // The user has canceled the dialog.
        // This override is optional.
    }
};

然后使用构建器显示对话框:

new SlideDayTimePicker.Builder(getSupportFragmentManager())
    .setListener(listener)
    .setInitialDay(1)
    .setInitialHour(13)
    .setInitialMinute(30)
    .build()
    .show();

希望你觉得它很有用。

day of week and timepicker for android