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

本文详细介绍了如何在Android应用中使用日历控件,包括启动Activity、设置不同选择模式、自定义视图和装饰器等功能。通过示例代码展示了如何初始化、配置和交互日历组件,同时提供了XML布局文件和相关资源的展示。
摘要由CSDN通过智能技术生成

本文实例为大家分享了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 modeButtons = new LinkedHashSet();

@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.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 dates = new ArrayList();

for (int i = 0; i < 5; i++) {

today.add(Calendar.DAY_OF_MONTH, 3);

dates.add(today.getTime());

}

calendar.setDecorators(Collections.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 dates = new ArrayList();

today.add(Calendar.DATE, 3);

dates.add(today.getTime());

today.add(Calendar.DATE, 5);

dates.add(today.getTime());

calendar.setDecorators(Collections.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.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.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.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内容:

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

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

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:

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

day_view_custom.xml

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="4dp"

>

android:id="@+id/day_view"

android:layout_width="match_parent"

android:layout_height="wrap_content"

style="@style/CalendarCell.CalendarDate"/>

android:layout_width="20dp"

android:layout_height="20dp"

android:src="@drawable/icon"

android:scaleType="centerInside"/>

dialog.xml

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:

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

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="8dp"

android:scrollbarStyle="outsideInset">

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:id="@+id/button_single"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/Single"

android:enabled="false"/>

android:id="@+id/button_multi"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/Multi"/>

android:id="@+id/button_range"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/Range"/>

android:id="@+id/button_display_only"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/DisplayOnly"/>

android:id="@+id/button_dialog"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/Dialog"/>

android:id="@+id/button_customized"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/Customized"/>

android:id="@+id/button_decorator"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/Decorator"/>

android:id="@+id/button_hebrew"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/Hebrew"/>

android:id="@+id/button_arabic"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/Arabic"/>

android:id="@+id/button_custom_view"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/CustomView"/>

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"

/>

android:id="@+id/done_button"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="@string/Done"

/>

资源结构如图:

b6b297ebdf95de3c0b988a4cd93eb0ad.png

strings.xml

Timessquare

Done

Customized

Decorator

Hebrew

Arabic

Custom View

Dialog

DisplayOnly

Range

Multi

Single

运行结果如图:

feb1cfd7a47b07afd1868d6c524b55fd.png

c18abeb6702fc57debc4507d69e2980e.png

52eb246869d3295b280b0939d74ad368.png

12f24ea45efc5d18b43b1f5e54e281af.png

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值