本教程为大家分享了AndroID日历库的使用方法,供大家参考,具体内容如下
MainActivity.java代码:
package siso.weekv;import androID.content.Intent;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;public class MainActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); findVIEwByID(R.ID.buttonBasic).setonClick@R_403_6818@ener(new VIEw.OnClick@R_403_6818@ener() { @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent(MainActivity.this,BasicActivity.class); startActivity(intent); } }); findVIEwByID(R.ID.buttonAsynchronous).setonClick@R_403_6818@ener(new VIEw.OnClick@R_403_6818@ener() { @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent(MainActivity.this,AsynchronousActivity.class); startActivity(intent); } }); }}
AsynchronousActivity.java代码:
package siso.weekv;import androID.Widget.Toast;import java.util.Array@R_403_6818@;import java.util.Calendar;import java.util.@R_403_6818@;import retrofit.Callback;import retrofit.RestAdapter;import retrofit.RetrofitError;import retrofit.clIEnt.Response;import siso.weekv.apiclient.Event;import siso.weekv.apiclient.MyJsonService;import siso.wvlib.WeekVIEwEvent;/** * An example of how events can be fetched from network and be displayed on the week vIEw. * Created by Raquib-ul-Alam Kanak on 1/3/2014. * Website: http://alamkanak.github.io */public class AsynchronousActivity extends BaseActivity implements Callback<@R_403_6818@<Event>> { private @R_403_6818@<WeekVIEwEvent> events = new Array@R_403_6818@<WeekVIEwEvent>(); boolean callednetwork = false; @OverrIDe public @R_403_6818@<? extends WeekVIEwEvent> onMonthChange(int newYear,int newMonth) { // Download events from network if it hasn't been done already. To understand how events are // downloaded using retrofit,visit http://square.github.io/retrofit if (!callednetwork) { RestAdapter retrofit = new RestAdapter.Builder() .setEndpoint("https://API.myJson.com/bins") .build(); MyJsonService service = retrofit.create(MyJsonService.class); service.@R_403_6818@Events(this); callednetwork = true; } // Return only the events that matches newYear and newMonth. @R_403_6818@<WeekVIEwEvent> matchedEvents = new Array@R_403_6818@<WeekVIEwEvent>(); for (WeekVIEwEvent event : events) { if (eventMatches(event,newYear,newMonth)) { matchedEvents.add(event); } } return matchedEvents; } /** * Checks if an event falls into a specific year and month. * @param event The event to check for. * @param year The year. * @param month The month. * @return True if the event matches the year and month. */ private boolean eventMatches(WeekVIEwEvent event,int year,int month) { return (event.getStartTime().get(Calendar.YEAR) == year && event.getStartTime().get(Calendar.MONTH) == month - 1) || (event.getEndTime().get(Calendar.YEAR) == year && event.getEndTime().get(Calendar.MONTH) == month - 1); } @OverrIDe public voID success(@R_403_6818@<Event> events,Response response) { this.events.clear(); for (Event event : events) { this.events.add(event.toWeekVIEwEvent()); } getWeekVIEw().notifyDatasetChanged(); } @OverrIDe public voID failure(RetrofitError error) { error.printstacktrace(); Toast.makeText(this,R.string.async_error,Toast.LENGTH_SHORT).show(); }}
BaseActivity.java
package siso.weekv;import androID.graphics.RectF;import androID.os.Bundle;import androID.support.v7.app.AppCompatActivity;import androID.util.TypedValue;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.Widget.Toast;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Locale;import siso.wvlib.DateTimeInterpreter;import siso.wvlib.MonthLoader;import siso.wvlib.WeekVIEw;import siso.wvlib.WeekVIEwEvent;public abstract class BaseActivity extends AppCompatActivity implements WeekVIEw.EventClick@R_403_6818@ener,MonthLoader.Monthchangelistener,WeekVIEw.EventLongPress@R_403_6818@ener,WeekVIEw.EmptyVIEwLongPress@R_403_6818@ener { private static final int TYPE_DAY_VIEW = 1; private static final int TYPE_THREE_DAY_VIEW = 2; private static final int TYPE_WEEK_VIEW = 3; private int mWeekVIEwType = TYPE_THREE_DAY_VIEW; private WeekVIEw mWeekVIEw; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_base); // Get a reference for the week vIEw in the layout. mWeekVIEw = (WeekVIEw) findVIEwByID(R.ID.weekVIEw); // Show a toast message about the touched event. mWeekVIEw.setonEventClick@R_403_6818@ener(this); mWeekVIEw.setMonthchangelistener(this); // Set long press @R_403_6818@ener for events. mWeekVIEw.setEventLongPress@R_403_6818@ener(this); mWeekVIEw.setEmptyVIEwLongPress@R_403_6818@ener(this); setupDateTimeInterpreter(false); } @OverrIDe public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main,menu); return true; } @OverrIDe public boolean onoptionsItemSelected(MenuItem item) { int ID = item.getItemID(); setupDateTimeInterpreter(ID == R.ID.action_week_vIEw); switch (ID){ case R.ID.action_today: mWeekVIEw.goToToday(); return true; case R.ID.action_day_vIEw: if (mWeekVIEwType != TYPE_DAY_VIEW) { item.setChecked(!item.isChecked()); mWeekVIEwType = TYPE_DAY_VIEW; mWeekVIEw.setNumberOfVisibleDays(1); // Lets change some dimensions to best fit the vIEw. mWeekVIEw.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,8,getResources().getdisplayMetrics())); mWeekVIEw.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,12,getResources().getdisplayMetrics())); mWeekVIEw.setEventTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,getResources().getdisplayMetrics())); } return true; case R.ID.action_three_day_vIEw: if (mWeekVIEwType != TYPE_THREE_DAY_VIEW) { item.setChecked(!item.isChecked()); mWeekVIEwType = TYPE_THREE_DAY_VIEW; mWeekVIEw.setNumberOfVisibleDays(3); // Lets change some dimensions to best fit the vIEw. mWeekVIEw.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,getResources().getdisplayMetrics())); } return true; case R.ID.action_week_vIEw: if (mWeekVIEwType != TYPE_WEEK_VIEW) { item.setChecked(!item.isChecked()); mWeekVIEwType = TYPE_WEEK_VIEW; mWeekVIEw.setNumberOfVisibleDays(7); mWeekVIEw.setColumnGap((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,2,10,getResources().getdisplayMetrics())); } return true; } return super.onoptionsItemSelected(item); } private voID setupDateTimeInterpreter(final boolean shortDate) { mWeekVIEw.setDateTimeInterpreter(new DateTimeInterpreter() { @OverrIDe public String interpretDate(Calendar date) { SimpleDateFormat weekdaynameFormat = new SimpleDateFormat("EEE",Locale.getDefault()); String weekday = weekdaynameFormat.format(date.getTime()); SimpleDateFormat format = new SimpleDateFormat(" M/d",Locale.getDefault()); if (shortDate) weekday = String.valueOf(weekday.charat(0)); return weekday.toupperCase() + format.format(date.getTime()); } @OverrIDe public String interpretTime(int hour) { return hour > 11 ? (hour - 12) + " PM" : (hour == 0 ? "12 AM" : hour + " AM"); } }); } protected String getEventTitle(Calendar time) { return String.format("事件 %02d:%02d %s/%d",time.get(Calendar.HOUR_OF_DAY),time.get(Calendar.MINUTE),time.get(Calendar.MONTH)+1,time.get(Calendar.DAY_OF_MONTH)); } @OverrIDe public voID onEventClick(WeekVIEwEvent event,RectF eventRect) { Toast.makeText(this,"点击 " + event.getname(),Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onEventLongPress(WeekVIEwEvent event,"长按事件: " + event.getname(),Toast.LENGTH_SHORT).show(); } @OverrIDe public voID onEmptyVIEwLongPress(Calendar time) { Toast.makeText(this,"空视图长按: " + getEventTitle(time),Toast.LENGTH_SHORT).show(); } public WeekVIEw getWeekVIEw() { return mWeekVIEw; }}
BasicActivity.java
package siso.weekv;import java.util.Array@R_403_6818@;import java.util.Calendar;import java.util.@R_403_6818@;import siso.wvlib.WeekVIEwEvent;public class BasicActivity extends BaseActivity { @OverrIDe public @R_403_6818@<? extends WeekVIEwEvent> onMonthChange(int newYear,int newMonth) { // Populate the week vIEw with some events. @R_403_6818@<WeekVIEwEvent> events = new Array@R_403_6818@<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); startTime = Calendar.getInstance(); startTime.set(Calendar.HOUR_OF_DAY,30); startTime.set(Calendar.MONTH,newYear); endTime = (Calendar) startTime.clone(); endTime.set(Calendar.HOUR_OF_DAY,4); endTime.set(Calendar.MINUTE,30); endTime.set(Calendar.MONTH,newMonth-1); event = new WeekVIEwEvent(10,endTime); event.setcolor(getResources().getcolor(R.color.event_color_02)); events.add(event); startTime = Calendar.getInstance(); startTime.set(Calendar.HOUR_OF_DAY,4); startTime.set(Calendar.MINUTE,20); startTime.set(Calendar.MONTH,5); endTime.set(Calendar.MINUTE,0); event = new WeekVIEwEvent(10,endTime); event.setcolor(getResources().getcolor(R.color.event_color_03)); events.add(event); startTime = Calendar.getInstance(); startTime.set(Calendar.HOUR_OF_DAY,5); startTime.set(Calendar.MINUTE,newYear); endTime = (Calendar) startTime.clone(); endTime.add(Calendar.HOUR_OF_DAY,2); endTime.set(Calendar.MONTH,newMonth-1); event = new WeekVIEwEvent(2,newYear); startTime.add(Calendar.DATE,1); endTime = (Calendar) startTime.clone(); endTime.add(Calendar.HOUR_OF_DAY,3); endTime.set(Calendar.MONTH,newMonth - 1); event = new WeekVIEwEvent(3,endTime); event.setcolor(getResources().getcolor(R.color.event_color_03)); events.add(event); startTime = Calendar.getInstance(); startTime.set(Calendar.DAY_OF_MONTH,15); startTime.set(Calendar.HOUR_OF_DAY,3); event = new WeekVIEwEvent(4,endTime); event.setcolor(getResources().getcolor(R.color.event_color_04)); events.add(event); startTime = Calendar.getInstance(); startTime.set(Calendar.DAY_OF_MONTH,1); startTime.set(Calendar.HOUR_OF_DAY,3); event = new WeekVIEwEvent(5,endTime); event.setcolor(getResources().getcolor(R.color.event_color_01)); events.add(event); startTime = Calendar.getInstance(); startTime.set(Calendar.DAY_OF_MONTH,startTime.getActualMaximum(Calendar.DAY_OF_MONTH)); startTime.set(Calendar.HOUR_OF_DAY,15); startTime.set(Calendar.MINUTE,endTime); event.setcolor(getResources().getcolor(R.color.event_color_02)); events.add(event); return events; }}
apiclient文件夹下Event.java和MyJsonService.java
package siso.weekv.apiclient;import androID.annotation.Suppresslint;import androID.graphics.color;import com.Google.gson.annotations.Expose;import com.Google.gson.annotations.Serializedname;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import siso.wvlib.WeekVIEwEvent;public class Event { @Expose @Serializedname("name") private String mname; @Expose @Serializedname("dayOfMonth") private int mDayOfMonth; @Expose @Serializedname("startTime") private String mStartTime; @Expose @Serializedname("endTime") private String mEndTime; @Expose @Serializedname("color") private String mcolor; public String getname() { return mname; } public voID setname(String name) { this.mname = name; } public int getDayOfMonth() { return mDayOfMonth; } public voID setDayOfMonth(int dayOfMonth) { this.mDayOfMonth = dayOfMonth; } public String getStartTime() { return mStartTime; } public voID setStartTime(String startTime) { this.mStartTime = startTime; } public String getEndTime() { return mEndTime; } public voID setEndTime(String endTime) { this.mEndTime = endTime; } public String getcolor() { return mcolor; } public voID setcolor(String color) { this.mcolor = color; } @Suppresslint("SimpleDateFormat") public WeekVIEwEvent toWeekVIEwEvent(){ // Parse time. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm"); Date start = new Date(); Date end = new Date(); try { start = sdf.parse(getStartTime()); } catch (ParseException e) { e.printstacktrace(); } try { end = sdf.parse(getEndTime()); } catch (ParseException e) { e.printstacktrace(); } // Initialize start and end time. Calendar Now = Calendar.getInstance(); Calendar startTime = (Calendar) Now.clone(); startTime.setTimeInMillis(start.getTime()); startTime.set(Calendar.YEAR,Now.get(Calendar.YEAR)); startTime.set(Calendar.MONTH,Now.get(Calendar.MONTH)); startTime.set(Calendar.DAY_OF_MONTH,getDayOfMonth()); Calendar endTime = (Calendar) startTime.clone(); endTime.setTimeInMillis(end.getTime()); endTime.set(Calendar.YEAR,startTime.get(Calendar.YEAR)); endTime.set(Calendar.MONTH,startTime.get(Calendar.MONTH)); endTime.set(Calendar.DAY_OF_MONTH,startTime.get(Calendar.DAY_OF_MONTH)); // Create an week vIEw event. WeekVIEwEvent weekVIEwEvent = new WeekVIEwEvent(); weekVIEwEvent.setname(getname()); weekVIEwEvent.setStartTime(startTime); weekVIEwEvent.setEndTime(endTime); weekVIEwEvent.setcolor(color.parsecolor(getcolor())); return weekVIEwEvent; }}
package siso.weekv.apiclient;import java.util.@R_403_6818@;import retrofit.Callback;import retrofit.http.GET;/** * Created by Raquib-ul-Alam Kanak on 1/3/16. * Website: http://alamkanak.github.io */public interface MyJsonService { //https://API.myJson.com/bins/3nh0k @GET("/3nh0k") voID @R_403_6818@Events(Callback<@R_403_6818@<Event>> eventsCallback);}
app项目结构:
Andriod类库项目结构:
项目运行结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android学习教程之日历库使用(15)全部内容,希望文章能够帮你解决Android学习教程之日历库使用(15)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)