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

基本設(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è)置該組件總共顯示四個星期-->
<!--3.并對該組件的星期盡心了定制-->
<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>
在主活動中,為其添加監(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é)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android實現(xiàn)系統(tǒng)重新啟動的功能
有些Android版本沒有系統(tǒng)重啟的功能,非常不方便。需要我們自己開發(fā)一個能夠重新啟動的應(yīng)用2013-11-11
Android 6.0區(qū)別U盤和SD卡設(shè)備的方法詳解
今天小編就為大家分享一篇Android 6.0區(qū)別U盤和SD卡設(shè)備的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Android 修改Preferences默認(rèn)樣式的步驟
這篇文章主要介紹了Android 修改Preferences默認(rèn)樣式的步驟,幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-04-04
Android自定義View實現(xiàn)兩種二維碼的掃描效果
這篇文章主要為大家詳細(xì)介紹了Android如何自定義View實現(xiàn)兩種二維碼的掃描效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
android scrollview 滑動到頂端或者指定位置的實現(xiàn)方法
下面小編就為大家?guī)硪黄猘ndroid scrollview 滑動到頂端或者指定位置的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04

