CalendarView功能與用法(日曆檢視)
阿新 • • 發佈:2019-01-29
簡單介紹一下CalendarView功能與用法
佈局程式碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id ="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請選擇您的生日"/>
<CalendarView android:id="@+id/cal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:firstDayOfWeek="3"
android:shownWeekCount ="7"
android:focusedMonthDateColor="#FF8000"
android:selectedWeekBackgroundColor="#9BFFFF"
android:weekSeparatorLineColor="#0000FF"/>
</LinearLayout>
實現程式碼
package com.test.calendarview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.Toast;
import android.widget.CalendarView.OnDateChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private CalendarView calendarView;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calendarView = (CalendarView)findViewById(R.id.cal);
textView = (TextView)findViewById(R.id.tv);
calendarView.setOnDateChangeListener(new OnDateChangeListener() {
@Override
public void onSelectedDayChange(CalendarView view, int year, int month,
int dayOfMonth) {
Toast.makeText(MainActivity.this,
"您的生日是"+year+"年"+month+"月"+dayOfMonth+"日", Toast.LENGTH_LONG).show();
}
});
}
}