android - Android 中用于重复事件的星期几和时间选择器

标签 android android-timepicker

我想创建一个控件,允许用户在我的 Android Activity 中选择星期几(星期一)和一天中的某个时间(下午 1:00)。找不到关于此的任何好帖子?

最佳答案

好的,我想我明白了。我只是不喜欢这个解决方案,因为我在星期几使用的 Spinner 与时间选择器的“主题”不匹配,所以它看起来很糟糕。

编辑:我更改了微调器的布局,您可以在帖子底部看到它,现在看起来好多了。

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"/>

关于android - Android 中用于重复事件的星期几和时间选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20746624/

相关文章:

java - 请一些 Java 帮助处理 Android 源代码?

java - csv文件到android中的JSONArray

android - 从json解析获取坐标并在mapview中显示?

android - ScrollView 与 TimePicker 的滚动打架,结果 Timepicker 不滚动

javascript - 如何更改默认配色方案 timepicker 对话框 fragment ?

android - 应用程式无法运作,很遗憾,XML已停止发生错误

android - 使用 facebook android sdk 发送消息

android - 如何在 Material Design android 中自定义 TimePicker?

java - 当另一个 fragment 的按钮尝试打开它时,时间选择器对话框会使应用程序崩溃

android - 将时间选择器添加到日期选择器android