日歷視圖(Calendarview)
常用屬性:
android:selectedWeekBackgroundColor(設置被選中周的背景顏色)
android:showWeekNumber(設置是否顯示第幾周)
android:unfocusedMonthDateColor(設置沒有焦點的月份的日期文字的顏色)
android:weekDaytextAppearance(設置星期幾的文字樣式)
android:weekNumberColor(設置顯示周編號的顏色)
android:weekSeparatorLineColor(設置周分割線的顏色)
監聽方法:setOnDatChangeListener
監聽器:CalendarView.OnDateChangeListener
下面我們直接看代碼:
1.Activity
//日歷視圖 public class CalendarViewActivity extends Activity { private Context context; private CalendarView calendarView; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.calendar_view); context = this; calendarView = (CalendarView)findViewById(R.id.calendarViewId); calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) { String content = year+"-"+(month+1)+"-"+dayOfMonth; Toast.makeText(context, "你選擇了:\n"+content, Toast.LENGTH_SHORT).show(); } }); } }
2.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="5dp" > <!-- 日歷視圖 --> <CalendarView android:id="@+id/calendarViewId" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
3.效果顯示圖