欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android学习教程之日历控件使用(7)

程序员文章站 2024-03-01 17:01:34
本文实例为大家分享了android日历控件的使用方法,供大家参考,具体内容如下 mainactivity.java代码: package siso.timess...

本文实例为大家分享了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!", r.layout.dialog);
        dialogview.init(lastyear.gettime(), nextyear.gettime(), new locale("iw", "il")) //
            .withselecteddate(new date());
      }
    });

    arabic.setonclicklistener(new onclicklistener() {
      @override public void onclick(view view) {
        showcalendarindialog("i'm arabic!", r.layout.dialog);
        dialogview.init(lastyear.gettime(), nextyear.gettime(), 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), 0, 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'), 'proguard-rules.pro'
    }
  }
}

dependencies {
  compile filetree(include: ['*.jar'], 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"
    style="@style/calendarcell.calendardate"/>

  <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>

资源结构如图:

Android学习教程之日历控件使用(7)

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)

Android学习教程之日历控件使用(7)

Android学习教程之日历控件使用(7)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。