我想在android中选择日历中的多个日期

时间:2014-08-04 10:45:07

标签: android

我想在日历中选择多个日期,多个选择工作正常但在吐司中只显示单个日期(第一个日期)。如果我选​​择了4天(1.8.14到4.8.14),所有日期都应该显示在吐司中。 这是我的代码


public class SampleTimesSquareActivity extends Activity{ 

          private static final String TAG="SampleTimesSquareActivity";
          private CalendarPickerView calendar;
          private AlertDialog theDialog;
          private CalendarPickerView dialogView;
          public static Date JULY;

          @Override
          protected void onCreate(Bundle savedInstanceState)
          {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.sample_calendar_picker);

                   final Calendar nextYear = Calendar.getInstance();
                    nextYear.add(Calendar.YEAR, 1);

                    final Calendar lastYear = Calendar.getInstance();
                    lastYear.add(Calendar.YEAR, -1);

                    calendar = (CalendarPickerView) findViewById(R.id.calendar_view);

                    final Button multi = (Button) findViewById(R.id.button_multi);



                        Calendar today = Calendar.getInstance();

                        today.set(Calendar.DAY_OF_MONTH,
                        today.getActualMaximum(Calendar.DAY_OF_MONTH));

                        Date end = today.getTime();
                        ArrayList<Date> dates = new ArrayList<Date>();
                        for (int i = 0; i < 5; i++) 
                        {
                          today.add(Calendar.DAY_OF_MONTH, 3);
                          dates.add(today.getTime());
                        }
                        calendar.init(new Date(),end) //
                            .inMode(SelectionMode.MULTIPLE);


                    findViewById(R.id.done_button).setOnClickListener(new OnClickListener() 
                    {
                          @Override
                          public void onClick(View view) 
                          {
                            Log.d(TAG, "Selected time in millis: " + calendar.getSelectedDate().getTime());
                            String toast = "Selected dates: " + calendar.getSelectedDate();
                            Toast.makeText(SampleTimesSquareActivity.this, toast, LENGTH_SHORT).show();
                          }
                    });
          }

          @Override public void onConfigurationChanged(Configuration newConfig) 
          {
                    boolean applyFixes = theDialog != null && theDialog.isShowing();
                    if (applyFixes) 
                    {
                      Log.d(TAG, "Config change: unfix the dimens so I'll get remeasured!");
                      dialogView.unfixDialogDimens();
                    }
                    super.onConfigurationChanged(newConfig);
                    if (applyFixes) 
                    {
                      dialogView.post(new Runnable() 
                      {
                        @Override public void run()
                        {
                          Log.d(TAG, "Config change done: re-fix the dimens!");
                          dialogView.fixDialogDimens();
                        }
                      });
                    }
          }
}

3 个答案:

答案 0 :(得分:3)

快速谷歌称CalendarPickerView中有一个名为getSelectedDates()的方法,它返回一个日期列表。这可能就是你想要的。

更新了链接:https://github.com/square/android-times-square/blob/master/library/src/main/java/com/squareup/timessquare/CalendarPickerView.java

答案 1 :(得分:0)

使用SelectionMode.MULTIPLE

calendar.init(today, nextYear.getTime())
        .inMode(CalendarPickerView.SelectionMode.MULTIPLE);

findViewById(R.id.btn_done).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
    Log.d("TAGGG", "Selected time in millis: " + calendar.getSelectedDate().getTime());
    String toast = "Selected: " + calendar.getSelectedDates();
    Toast.makeText(CalenderItem.this, toast, Toast.LENGTH_SHORT).show();
}
});

答案 2 :(得分:0)

尝试以下代码以简要说明.......

final Calendar nextYear = Calendar.getInstance();
    nextYear.add(Calendar.YEAR, 2);


     gson = new Gson();
    sharedpreferences1 = getSharedPreferences("MyPREFERENCES10005471hjsdhjsdjghjfdjksdlsasa00fdsadeetytadsadsywrerwerrw0255878762343", Context.MODE_PRIVATE);
    editor = sharedpreferences1.edit();
    calendar = (CalendarPickerView) findViewById(R.id.calendar_view);
    final String ddd=sharedpreferences1.getString("Str",null);
    sdf = new SimpleDateFormat("dd-MM-yyyy");
    Type type = new TypeToken<ArrayList<Date>>() {}.getType();
    arrayList = gson.fromJson(ddd, type);
    dates = new ArrayList<Date>();
    if(arrayList!=null){
        Toast.makeText(SampleTimesSquareActivity.this, "" + arrayList, LENGTH_SHORT).show();
        calendar.init(new Date(), nextYear.getTime()) //
                .inMode(SelectionMode.MULTIPLE) //
                .withSelectedDates(arrayList);
        for(int ii=0;ii<arrayList.size();ii++){
            Date d=arrayList.get(ii);
            String format=sdf.format(d);
            Toast.makeText(getApplicationContext(),format,Toast.LENGTH_SHORT).show();
        }
    }
    else{
        Toast.makeText(SampleTimesSquareActivity.this, "Null", LENGTH_SHORT).show();
        calendar.init(new Date(), nextYear.getTime()) //
                .inMode(SelectionMode.MULTIPLE) //
                .withSelectedDates(arrayList);
    }



    findViewById(R.id.done_button).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d(TAG, "Selected time in millis: " + calendar.getSelectedDate().getTime());
            String toast = "Selected: " + calendar.getSelectedDate();
             dates = (ArrayList<Date>) calendar.getSelectedDates();
             String json=null;
            Log.i("Size",""+dates);
            for (int i = 0; i < dates.size(); i++) {
                Date tempDate = dates.get(i);
                String formattedDate = sdf.format(tempDate);
                Toast.makeText(SampleTimesSquareActivity.this, "" + formattedDate, LENGTH_SHORT).show();
                 json = gson.toJson(dates);

            }

            editor.putString("Str", json);
            editor.clear();
            editor.apply();
        }
    });

谢谢......享受