Android CalendarView 使用示例

定义布局文件:




    

    


主程序文件代码:

package com.toby.personal.testlistview;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.CalendarView;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    final private static String TAG = "Toby_Test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final CalendarView calendarView = (CalendarView) findViewById(R.id.calendarView);
        final TextView selectData = (TextView) findViewById(R.id.selectDate);

        final Calendar c = Calendar.getInstance();
        selectData.setText(getText(R.string.selectDate) + " " +
                String.valueOf(c.get(Calendar.YEAR)) + "/" +
                String.valueOf(c.get(Calendar.MONTH)) + "/" +
                String.valueOf(c.get(Calendar.DATE)));

        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView,
                                            int year, int month, int dayOfMonth) {
                selectData.setText(getText(R.string.selectDate) + " " + String.valueOf(year) + "/" +
                        String.valueOf(month) + "/" + String.valueOf(dayOfMonth));
            }
        });
    }

}

本示例的运行效果:


Android CalendarView 使用示例_第1张图片
运行效果

参考文献:《疯狂Android讲义(第2版)》

你可能感兴趣的:(Android CalendarView 使用示例)