2013-12-23 50 views
3

我想創建一個控件,允許用戶選擇一週中的某一天(星期一)和一天中的某一天(下午1:00)在我的Android活動。無法找到任何好帖子?Android中的星期幾和時間選擇器,用於定期事件

+0

所以基本上你想要一個對話框,在同一對話框一個datepicker和TimePicker? –

+0

像谷歌日曆?當你選擇日期和時間 –

+0

不,我只需要一週中的某一天和一個時間,沒有日期。 – Jarrette

回答

1

好吧,我覺得我明白了。我只是不喜歡這個解決方案,因爲我在一週中使用的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"/> 
+3

你可以附上一張照片嗎? – bubakazouba

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