Day of week and Time picker in Android for a recur

2019-05-02 11:27发布

I'd like to create a control that allows a user to choose a day of the week (Monday) and a time of day (1:00 PM) in my Android activity. Not been able to find any good posts on this?

3条回答
Ridiculous、
2楼-- · 2019-05-02 11:32

I've combined a day of week and time picker in a single dialog.

You can check out the library here

You first create a listener:

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.
    }
};

Then you use the builder to show the dialog:

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

Hope you find it useful.

day of week and timepicker for android

查看更多
The star\"
3楼-- · 2019-05-02 11:35

You could try to use some external library like android-betterpickers. It has, at least in my opinion, a great design:

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

查看更多
Melony?
4楼-- · 2019-05-02 11:55

OK, I think I figured it out. I just don't love this solution because the Spinner I use for the day of the week doesn't match the "theme" of the timepicker, so it looks really bad.

EDIT: I changed the layout for the spinner you can see it at the bottom of the post, looks much better now.

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"/>
查看更多
登录 后发表回答