本文實(shí)例講述了Android開發(fā)之日歷CalendarView用法。分享給大家供大家參考,具體如下:
簡介:
1.CalendarView是安卓自帶的一個(gè)日歷控件
2.在主活動(dòng)中 通過設(shè)置setOnDataChangeListener() 來為其添加監(jiān)聽事件
可在其中獲得 洪湖所選擇的年月日的 詳細(xì)信息
實(shí)例:

基本設(shè)置方法:
1. 日歷的整體背景顏色 android:selectedWeekBackgroundColor="#aff"
2. 月份選擇部分的背景色 android:focusedMonthDateColor="#f00"
3. 顯示星期的背景色 android:weekSeparatorLineColor="#ff0"
4. 被選中的日期的背景色 android:unfocusedMonthDateColor="#f9f"
這里給出它的布局文件中的調(diào)用與配置:
<?xml version="1.0" encoding="utf-8" ?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:text="please choose your birthday :" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="15dp" android:typeface="monospace"/> <!--1.設(shè)置以星期二為每周第一天--> <!--2.設(shè)置該組件總共顯示四個(gè)星期--> <!--3.并對(duì)該組件的星期盡心了定制--> <CalendarView android:id="@+id/calenderView" android:layout_width="match_parent" android:layout_height="match_parent" android:firstDayOfWeek="3" android:shownWeekCount="4" android:selectedWeekBackgroundColor="#aff" android:focusedMonthDateColor="#f00" android:weekSeparatorLineColor="#ff0" android:unfocusedMonthDateColor="#f9f"> </CalendarView></LinearLayout>
在主活動(dòng)中,為其添加監(jiān)聽事件后
可以通過 day month dayOfMonth 來獲得用戶選擇的日期的具體信息:
public class MainActivity extends Activity { CalendarView calendarView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); calendarView = (CalendarView) findViewById(R.id.calenderView); //calendarView 監(jiān)聽事件 calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { @Override public void onSelectedDayChange( CalendarView view, int year, int month, int dayOfMonth) { //顯示用戶選擇的日期 Toast.makeText(MainActivity.this,year + "年" + month + "月" + dayOfMonth + "日",Toast.LENGTH_SHORT).show(); } }); }}參考自瘋狂Android講義。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選