
本文实例为大家分享了AndroID日历控件的使用方法,供大家参考,具体内容如下
MainActivity.java代码:
package siso.timessquare;import androID.content.Intent;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;public class MainActivity extends AppCompatActivity { private button btntimesSquare; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); btntimesSquare=(button)findVIEwByID(R.ID.btntimesSquare); btntimesSquare.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent(); intent.setClass(MainActivity.this,SampleTimesSquareActivity.class); //直接启动一个Activity startActivity(intent); } }); }}SampleTimesSquareActivity.java代码:
package siso.timessquare;import androID.app.Activity;import androID.app.AlertDialog;import androID.content.DialogInterface;import androID.content.res.Configuration;import androID.os.Bundle;import androID.util.Log;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.Widget.button;import androID.Widget.Toast;import java.util.ArrayList;import java.util.Arrays;import java.util.Calendar;import java.util.Collections;import java.util.Date;import java.util.linkedHashSet;import java.util.Locale;import java.util.Set;import siso.datelibrary.CalendarCellDecorator;import siso.datelibrary.CalendarPickerVIEw;import siso.datelibrary.DefaultDayVIEwAdapter;import static androID.Widget.Toast.LENGTH_SHORT;public class SampleTimesSquareActivity extends Activity { private static final String TAG = "SampleTimesSquareActivi"; private CalendarPickerVIEw calendar; private AlertDialog theDialog; private CalendarPickerVIEw dialogVIEw; private final Set<button> modebuttons = new linkedHashSet<button>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.sample_calendar_picker); final Calendar nextYear = Calendar.getInstance(); nextYear.add(Calendar.YEAR,1); final Calendar lastYear = Calendar.getInstance(); lastYear.add(Calendar.YEAR,-1); calendar = (CalendarPickerVIEw) findVIEwByID(R.ID.calendar_vIEw); calendar.init(lastYear.getTime(),nextYear.getTime()) // .inMode(CalendarPickerVIEw.SelectionMode.SINGLE) // .withSelectedDate(new Date()); initbuttonListeners(nextYear,lastYear); } private voID initbuttonListeners(final Calendar nextYear,final Calendar lastYear) { final button single = (button) findVIEwByID(R.ID.button_single); final button multi = (button) findVIEwByID(R.ID.button_multi); final button range = (button) findVIEwByID(R.ID.button_range); final button displayOnly = (button) findVIEwByID(R.ID.button_display_only); final button dialog = (button) findVIEwByID(R.ID.button_dialog); final button customized = (button) findVIEwByID(R.ID.button_customized); final button decorator = (button) findVIEwByID(R.ID.button_decorator); final button hebrew = (button) findVIEwByID(R.ID.button_hebrew); final button arabic = (button) findVIEwByID(R.ID.button_arabic); final button customVIEw = (button) findVIEwByID(R.ID.button_custom_vIEw); modebuttons.addAll(Arrays.asList(single,multi,range,displayOnly,decorator,customVIEw)); single.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { setbuttonsEnabled(single); calendar.setCustomDayVIEw(new DefaultDayVIEwAdapter()); calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList()); calendar.init(lastYear.getTime(),nextYear.getTime()) // .inMode(CalendarPickerVIEw.SelectionMode.SINGLE) // .withSelectedDate(new Date()); } }); multi.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { setbuttonsEnabled(multi); calendar.setCustomDayVIEw(new DefaultDayVIEwAdapter()); Calendar today = Calendar.getInstance(); ArrayList<Date> dates = new ArrayList<Date>(); for (int i = 0; i < 5; i++) { today.add(Calendar.DAY_OF_MONTH,3); dates.add(today.getTime()); } calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList()); calendar.init(new Date(),nextYear.getTime()) // .inMode(CalendarPickerVIEw.SelectionMode.MulTIPLE) // .withSelectedDates(dates); } }); range.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { setbuttonsEnabled(range); calendar.setCustomDayVIEw(new DefaultDayVIEwAdapter()); Calendar today = Calendar.getInstance(); ArrayList<Date> dates = new ArrayList<Date>(); today.add(Calendar.DATE,3); dates.add(today.getTime()); today.add(Calendar.DATE,5); dates.add(today.getTime()); calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList()); calendar.init(new Date(),nextYear.getTime()) // .inMode(CalendarPickerVIEw.SelectionMode.RANGE) // .withSelectedDates(dates); } }); displayOnly.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { setbuttonsEnabled(displayOnly); calendar.setCustomDayVIEw(new DefaultDayVIEwAdapter()); calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList()); calendar.init(new Date(),nextYear.getTime()) // .inMode(CalendarPickerVIEw.SelectionMode.SINGLE) // .withSelectedDate(new Date()) // .displayOnly(); } }); dialog.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { String Title = "I'm a dialog!"; showCalendarInDialog(Title,R.layout.dialog); dialogVIEw.init(lastYear.getTime(),nextYear.getTime()) // .withSelectedDate(new Date()); } }); customized.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { showCalendarInDialog("Pimp my calendar!",R.layout.dialog_customized); dialogVIEw.init(lastYear.getTime(),nextYear.getTime()) .withSelectedDate(new Date()); } }); decorator.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { setbuttonsEnabled(decorator); calendar.setCustomDayVIEw(new DefaultDayVIEwAdapter()); calendar.setDecorators(Arrays.<CalendarCellDecorator>asList(new SampleDecorator())); calendar.init(lastYear.getTime(),nextYear.getTime()) // .inMode(CalendarPickerVIEw.SelectionMode.SINGLE) // .withSelectedDate(new Date()); } }); hebrew.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { showCalendarInDialog("I'm Hebrew!",nextYear.getTime(),new Locale("iw","IL")) // .withSelectedDate(new Date()); } }); arabic.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { showCalendarInDialog("I'm arabic!",new Locale("ar","EG")) // .withSelectedDate(new Date()); } }); customVIEw.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { setbuttonsEnabled(customVIEw); calendar.setDecorators(Collections.<CalendarCellDecorator>emptyList()); calendar.setCustomDayVIEw(new SampleDayVIEwAdapter()); calendar.init(lastYear.getTime(),nextYear.getTime()) .inMode(CalendarPickerVIEw.SelectionMode.SINGLE) .withSelectedDate(new Date()); } }); findVIEwByID(R.ID.done_button).setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw vIEw) { Log.d(TAG,"Selected time in millis: " + calendar.getSelectedDate().getTime()); String toast = "Selected: " + calendar.getSelectedDate().getTime(); Toast.makeText(SampleTimesSquareActivity.this,toast,LENGTH_SHORT).show(); } }); } private voID showCalendarInDialog(String Title,int layoutResID) { dialogVIEw = (CalendarPickerVIEw) getLayoutInflater().inflate(layoutResID,null,false); theDialog = new AlertDialog.Builder(this) // .setTitle(Title) .setVIEw(dialogVIEw) .setNeutralbutton("dismiss",new DialogInterface.OnClickListener() { @OverrIDe public voID onClick(DialogInterface dialogInterface,int i) { dialogInterface.dismiss(); } }) .create(); theDialog.setonShowListener(new DialogInterface.OnShowListener() { @OverrIDe public voID onShow(DialogInterface dialogInterface) { Log.d(TAG,"onShow: fix the dimens!"); dialogVIEw.fixDialogDimens(); } }); theDialog.show(); } private voID setbuttonsEnabled(button currentbutton) { for (button modebutton : modebuttons) { modebutton.setEnabled(modebutton != currentbutton); } } @OverrIDe public voID onConfigurationChanged(Configuration newConfig) { boolean applyFixes = theDialog != null && theDialog.isShowing(); if (applyFixes) { Log.d(TAG,"Config change: unfix the dimens so I'll get remeasured!"); dialogVIEw.unfixDialogDimens(); } super.onConfigurationChanged(newConfig); if (applyFixes) { dialogVIEw.post(new Runnable() { @OverrIDe public voID run() { Log.d(TAG,"Config change done: re-fix the dimens!"); dialogVIEw.fixDialogDimens(); } }); } }}SampleDayVIEwAdapter.java代码:
package siso.timessquare;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.Widget.TextVIEw;import siso.datelibrary.CalendarCellVIEw;import siso.datelibrary.DayVIEwAdapter;public class SampleDayVIEwAdapter implements DayVIEwAdapter { @OverrIDe public voID makeCellVIEw(CalendarCellVIEw parent) { VIEw layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.day_vIEw_custom,null); parent.addVIEw(layout); parent.setDayOfMonthTextVIEw((TextVIEw) layout.findVIEwByID(R.ID.day_vIEw)); }}SampleDecorator.java代码:
package siso.timessquare;import androID.text.SpannableString;import androID.text.Spanned;import androID.text.style.relativeSizeSpan;import java.util.Date;import siso.datelibrary.CalendarCellDecorator;import siso.datelibrary.CalendarCellVIEw;public class SampleDecorator implements CalendarCellDecorator { @OverrIDe public voID decorate(CalendarCellVIEw cellVIEw,Date date) { String dateString = Integer.toString(date.getDate()); SpannableString string = new SpannableString(dateString + "\nTitle"); string.setSpan(new relativeSizeSpan(0.5f),dateString.length(),Spanned.SPAN_INCLUSIVE_EXCLUSIVE); cellVIEw.getDayOfMonthTextVIEw().setText(string); }}activity_main.xml内容:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="siso.timessquare.MainActivity"> <button androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="AndroID 日历控件" androID:ID="@+ID/btntimesSquare" androID:layout_alignParenttop="true" androID:layout_centerHorizontal="true" /></relativeLayout>
Module App下build.gradle内容:
apply plugin: 'com.androID.application'androID { compileSdkVersion 23 buildToolsversion "23.0.1" defaultConfig { applicationID "siso.timessquare" minSdkVersion 22 targetSdkVersion 22 versionCode 1 versionname "1.0" } buildTypes { release { MinifyEnabled false proguardfiles getDefaultProguardfile('proguard-androID.txt'),'proguard-rules.pro' } }}dependencIEs { compile filetree(include: ['*.jar'],dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.androID.support:appcompat-v7:23.0.1' compile project(path: ':datelibrary')}Module datelibrary下build.gradle内容:
apply plugin: 'com.androID.library'androID { compileSdkVersion 23 buildToolsversion "23.0.1" defaultConfig { minSdkVersion 22 targetSdkVersion 22 versionCode 1 versionname "1.0" } buildTypes { release { MinifyEnabled false proguardfiles getDefaultProguardfile('proguard-androID.txt'),dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.androID.support:appcompat-v7:23.0.1'}activity_sample_times_square.xml:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="siso.timessquare.SampleTimesSquareActivity"></relativeLayout>
day_vIEw_custom.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" androID:padding="4dp" > <TextVIEw androID:ID="@+ID/day_vIEw" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /> <ImageVIEw androID:layout_wIDth="20dp" androID:layout_height="20dp" androID:src="@drawable/icon" androID:scaleType="centerInsIDe"/></linearLayout>
dialog.xml
<com.squareup.timessquare.CalendarPickerVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:ID="@+ID/calendar_vIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingleft="16dp" androID:paddingRight="16dp" androID:paddingBottom="16dp" androID:scrollbarStyle="outsIDeOverlay" androID:clipTopadding="false" androID:background="#FFFFFF" />
dialog_customized.xml:
<com.squareup.timessquare.CalendarPickerVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" androID:ID="@+ID/calendar_vIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingleft="16dp" androID:paddingRight="16dp" androID:paddingBottom="16dp" androID:scrollbarStyle="outsIDeOverlay" androID:clipTopadding="false" androID:background="@color/custom_background" app:tsquare_dayBackground="@drawable/custom_calendar_bg_selector" app:tsquare_dayTextcolor="@color/custom_calendar_text_selector" app:tsquare_divIDercolor="@color/transparent" app:tsquare_TitleTextcolor="@color/custom_calendar_text_selector" app:tsquare_headerTextcolor="@color/custom_header_text" />
sample_calendar_picker.xml
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:orIEntation="vertical" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <horizontalscrollview androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margintop="8dp" androID:scrollbarStyle="outsIDeInset"> <linearLayout androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content"> <button androID:ID="@+ID/button_single" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/Single" androID:enabled="false"/> <button androID:ID="@+ID/button_multi" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/Multi"/> <button androID:ID="@+ID/button_range" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/Range"/> <button androID:ID="@+ID/button_display_only" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/displayOnly"/> <button androID:ID="@+ID/button_dialog" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/Dialog"/> <button androID:ID="@+ID/button_customized" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/Customized"/> <button androID:ID="@+ID/button_decorator" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/Decorator"/> <button androID:ID="@+ID/button_hebrew" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/Hebrew"/> <button androID:ID="@+ID/button_arabic" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/arabic"/> <button androID:ID="@+ID/button_custom_vIEw" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/CustomVIEw"/> </linearLayout> </horizontalscrollview> <siso.datelibrary.CalendarPickerVIEw androID:ID="@+ID/calendar_vIEw" androID:layout_wIDth="match_parent" androID:layout_height="0dp" androID:layout_weight="1" androID:paddingleft="16dp" androID:paddingRight="16dp" androID:paddingBottom="16dp" androID:scrollbarStyle="outsIDeOverlay" androID:clipTopadding="false" androID:background="#FFFFFF" /> <button androID:ID="@+ID/done_button" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:text="@string/Done" /></linearLayout>
资源结构如图:
strings.xml
<resources> <string name="app_name">Timessquare</string> <string name="Done">Done</string> <string name="Customized">Customized</string> <string name="Decorator">Decorator</string> <string name="Hebrew">Hebrew</string> <string name="arabic">arabic</string> <string name="CustomVIEw">Custom VIEw</string> <string name="Dialog">Dialog</string> <string name="displayOnly">displayOnly</string> <string name="Range">Range</string> <string name="Multi">Multi</string> <string name="Single">Single</string></resources>
运行结果如图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android学习教程之日历控件使用(7)全部内容,希望文章能够帮你解决Android学习教程之日历控件使用(7)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)