1.介绍
2.常用属性
3.xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /> <CalendarView android:id="@+id/calendarView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>
4.java后台
对应的工程名:test26
package com.lucky.test26calendar; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.CalendarView; import android.widget.Toast; import java.text.SimpleDateFormat; public class MainActivity extends AppCompatActivity { CalendarView calendarView; Button button; String timeStr=""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); calendarView=findViewById(R.id.calendarView); button=findViewById(R.id.button); //设置时间显示格式 SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //yyyy表示年,MM表示月,dd表示天,HH表示时,mm表示分,ss表示秒 timeStr=simpleDateFormat.format(calendarView.getDate()); //将calendarView获取的时间样式转化为simpleDateFormat所设置的样式 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,timeStr,Toast.LENGTH_SHORT).show(); } }); calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() { @Override public void onSelectedDayChange( CalendarView view, int year, int month, int dayOfMonth) { timeStr=year+"-"+month+"-"+dayOfMonth; } }); } }