在android中的日历日/月/周视图

时间:2014-10-25 06:40:10

标签: android android-calendar

我需要像附加照片一样的Android日历视图。我已经尝试过这个库     https://github.com/alamkanak/Android-Week-View用于日历视图。     这个是仅在日历中添加的固定静态事件。     我无法在此库日历视图中添加动态事件。     任何人都可以在android中建议任何自定义库。

enter image description here

示例日历视图库代码的Android-Week-View:

我在这个库中尝试了什么:

这是代码,在日历视图中静态添加事件。在运行此示例时,我可以在日历视图中查看特定日期的事件。

 @Override
 public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {

  // Populate the week view with some events.
    List<WeekViewEvent> events = new ArrayList<WeekViewEvent>();

    Calendar startTime = Calendar.getInstance();
    startTime.set(Calendar.HOUR_OF_DAY, 3);
    startTime.set(Calendar.MINUTE, 0);
    startTime.set(Calendar.MONTH, newMonth-1);
    startTime.set(Calendar.YEAR, newYear);
    Calendar endTime = (Calendar) startTime.clone();
    endTime.add(Calendar.HOUR, 1);
    endTime.set(Calendar.MONTH, newMonth-1);
    WeekViewEvent event = new    WeekViewEvent(1, getEventTitle(startTime),
    startTime, endTime);
    event.setColor(getResources().getColor(R.color.event_color_01));
    events.add(event);
      return events;
    }
    when i click this event on calendar page, its showing that event
    time and date in toast.
     @Override
    public void onEventClick(WeekViewEvent event, RectF eventRect) {
    Toast.makeText(MainActivity.this, "Clicked " 
    + event.getName(), Toast.LENGTH_SHORT).show();
    Log.v("heal", "Clicked "+ event.getName()) ;
   }

注意:

  • 我需要动态地在此日历视图中添加相同的事件。
  • 我不知道如何添加此活动。
  • 我只在onCreate()内添加一个事件。但是,该事件未在日历视图中显示,但值传递给该构造函数。任何人都可以帮我解决这个问题。

2 个答案:

答案 0 :(得分:3)

library you are using支持动态事件添加。检查this sample以获得更好的主意。

答案 1 :(得分:0)

尝试执行此操作,它会在用户单击一个空框时添加一个事件。您可以根据需要修改事件的参数。

public class MainActivity extends AppCompatActivity implements WeekView.EventClickListener,WeekView.EmptyViewClickListener, MonthLoader.MonthChangeListener {

WeekView mWeekView;
List<WeekViewEvent> events;
Calendar clickedTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWeekView=findViewById(R.id.weekView);
    mWeekView.setAccessibilityLiveRegion(MODE_APPEND);
    mWeekView.setNumberOfVisibleDays(7);

    events= new ArrayList<WeekViewEvent>();
    clickedTime=Calendar.getInstance();

    // Lets change some dimensions to best fit the view.
    mWeekView.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics()));
    mWeekView.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics()));
    mWeekView.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, getResources().getDisplayMetrics()));

    mWeekView.setMonthChangeListener(this);
    mWeekView.setEmptyViewClickListener(this);
    mWeekView.setOnEventClickListener(this);

}

@Override
public List<? extends WeekViewEvent> onMonthChange(int newYear, int newMonth) {
    //Toast.makeText(this, "newMonth: "+newMonth+"  newYear: "+newYear, Toast.LENGTH_SHORT).show();

    Calendar startTime = (Calendar) clickedTime.clone();
    startTime.set(Calendar.MINUTE, 0);
    startTime.set(Calendar.MONTH, newMonth);
    startTime.set(Calendar.YEAR, newYear);
    Calendar endTime = (Calendar) startTime.clone();
    endTime.add(Calendar.HOUR, 1);
    WeekViewEvent event = new WeekViewEvent(1, "Event Name", startTime, endTime);
    //event.setColor(getResources().getColor(R.color.event_color_02));
    events.add(event);
    return events;
}

@Override
public void onEmptyViewClicked(final Calendar time) {
    clickedTime=(Calendar) time.clone();
    mWeekView.notifyDatasetChanged();
    Log.i("msg","Empty box has been filled successfully.");
}
}