日曆視圖(CalendarView)

日曆視圖(Calendarview)android

經常使用屬性:佈局

android:selectedWeekBackgroundColor(設置被選中周的背景顏色)this

android:showWeekNumber(設置是否顯示第幾周)spa

android:unfocusedMonthDateColor(設置沒有焦點的月份的日期文字的顏色)code

android:weekDaytextAppearance(設置星期幾的文字樣式)xml

android:weekNumberColor(設置顯示周編號的顏色)blog

android:weekSeparatorLineColor(設置周分割線的顏色)utf-8

監聽方法:setOnDatChangeListenerit

監聽器:CalendarView.OnDateChangeListenerio

下面咱們直接看代碼:

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.效果顯示圖

相關文章
相關標籤/搜索