简单CalendarView日历控件使用

实现效果图:

简单CalendarView日历控件使用_第1张图片

(1)activity_main.xml,添加控件

<CalendarView
    android:id="@+id/calendarViewId"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
(2)在主界面里面添加点击事件,MainActivity

public class MainActivity extends Activity {

    private Context context;
    private CalendarView calendarView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.date);

        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();
            }
        });
    }

}

你可能感兴趣的:(android,学习)