日历视图(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.效果显示图

相关文章
相关标签/搜索