
#Region " 返回农历 "
'返回农历
'cCalendar.MaxSupportedDateTime 返回支持的最大日期,即2101-1-28
'cCalendar.MinSupportedDateTime 返回支持的最小日期,即190-2-19
Private cCalendar As New System.Globalization.ChineseLunisolarCalendar
Public Function PubFunGet_CNDate(ByVal sDateTime As Date) As String
cCalendar = New System.Globalization.ChineseLunisolarCalendar
Dim lyear As Integer = cCalendar.GetYear(sDateTime)
Dim lmonth As Integer = cCalendar.GetMonth(sDateTime)
Dim lday As Integer = cCalendar.GetDayOfMonth(sDateTime)
Dim lweek As Integer = cCalendar.GetDayOfWeek(sDateTime)
'获取闰月, 0 则表示没有闰月
Dim leapMonth As Integer = cCalendar.GetLeapMonth(lyear)
Dim isleap As Boolean = False
If (leapMonth >0) Then
If (leapMonth = lmonth) Then
'闰月
isleap = True
lmonth = lmonth - 1
ElseIf (lmonth >leapMonth) Then
lmonth = lmonth - 1
End If
End If
Return String.Concat(GetLunisolarYear(lyear), IIf(isleap = True, "闰年", "年"), GetLunisolarMonth(lmonth), "月", GetLunisolarDay(lday))
End Function
'十天干
Private tiangan As String() = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"}
'十二地支
Private dizhi As String() = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"}
'十二升哪生肖
Private shengxiao As String() = {"鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "枯笑轿猴", "鸡", "狗", "猪"}
'农历月
Private months As String() = {"正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)"}
'农历日
Private days1 As String() = {"初", "十", "廿", "三"}
Private days As String() = {"一", "二", "三", "四", "五", "六", "七", "八", "九", "十"}
'返回农历年(天干 地支 生肖)
Private Function GetLunisolarYear(ByVal year As Integer) As String
GetLunisolarYear = ""
If (year >3) Then
Dim tgIndex As Integer = (year - 4) Mod 10
Dim dzIndex As Integer = (year - 4) Mod 12
Return tiangan(tgIndex) &dizhi(dzIndex) &"[" &shengxiao(dzIndex) &"]"
End If
'无效的年份!
End Function
'没肆返回生肖
Private Function GetShengXiao(ByVal sDateTime As Date) As String
Return shengxiao(cCalendar.GetTerrestrialBranch(cCalendar.GetSexagenaryYear(sDateTime)) - 1)
End Function
'返回农历月
Private Function GetLunisolarMonth(ByVal month As Integer) As String
GetLunisolarMonth = ""
If (month <13 AndAlso month >0) Then
Return months(month - 1)
End If
'无效的月份!
End Function
'返回农历日
Private Function GetLunisolarDay(ByVal day As Integer) As String
GetLunisolarDay = ""
If (day >0 AndAlso day <32) Then
If (day <>20 AndAlso day <>30) Then
Return String.Concat(days1((day - 1) \ 10), days((day - 1) Mod 10))
Else
Return String.Concat(days((day - 1) \ 10), days1(1))
End If
End If
'无效的日!
End Function
#End Region
因为日历是系统自带的,所以读写它一定要申请权限,也拆衫就是在AndroidManifest.xml加如下两行代码(一个读一个写):<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
Android中日历用了三个URL,分别是日历用户的URL,事件的URL,事件提醒URL,三个URL在Android2.1之前是如下的样旅尺腔子:
calanderURL = "content://calendar/困亏calendars"
calanderEventURL = "content://calendar/events"
calanderRemiderURL= "content://calendar/reminders"
但是在Android2.2版本以后,三个URL有了改变,变成如下的样子:
calanderURL = "content://com.android.calendar/calendars"
calanderEventURL = "content://com.android.calendar/events"
calanderRemiderURL = "content://com.android.calendar/reminders"
简单的Demo,按照我的步骤一步一步的来:
第一步:新建一个Android工程命名为CalendarDemo.
第二步:修改main.xml布局文件,增加了三个按钮,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/readUserButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Get a User"
/>
<Button
android:id="@+id/readEventButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Get a Event"
/>
<Button
android:id="@+id/writeEventButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Input a Event"
/>
</LinearLayout>
第三步:修改主核心程序CalendarDemo.java,代码如下:
package com.tutor.calendardemo
import java.util.Calendar
import android.app.Activity
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.Toast
public class CalendarDemo extends Activity implements OnClickListener {
private Button mReadUserButton
private Button mReadEventButton
private Button mWriteEventButton
private static String calanderURL = ""
private static String calanderEventURL = ""
private static String calanderRemiderURL = ""
//为了兼容不同版本的日历,2.2以后url发生改变
static{
if(Integer.parseInt(Build.VERSION.SDK) >= 8){
calanderURL = "content://com.android.calendar/calendars"
calanderEventURL = "content://com.android.calendar/events"
calanderRemiderURL = "content://com.android.calendar/reminders"
}else{
calanderURL = "content://calendar/calendars"
calanderEventURL = "content://calendar/events"
calanderRemiderURL = "content://calendar/reminders"
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
setupViews()
}
private void setupViews(){
mReadUserButton = (Button)findViewById(R.id.readUserButton)
mReadEventButton = (Button)findViewById(R.id.readEventButton)
mWriteEventButton = (Button)findViewById(R.id.writeEventButton)
mReadUserButton.setOnClickListener(this)
mReadEventButton.setOnClickListener(this)
mWriteEventButton.setOnClickListener(this)
}
@Override
public void onClick(View v) {
if(v == mReadUserButton){
Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null,
null, null, null)
if(userCursor.getCount() >0){
userCursor.moveToFirst()
String userName = userCursor.getString(userCursor.getColumnIndex("name"))
Toast.makeText(CalendarDemo.this, userName, Toast.LENGTH_LONG).show()
}
}else if(v == mReadEventButton){
Cursor eventCursor = getContentResolver().query(Uri.parse(calanderEventURL), null,
null, null, null)
if(eventCursor.getCount() >0){
eventCursor.moveToLast()
String eventTitle = eventCursor.getString(eventCursor.getColumnIndex("title"))
Toast.makeText(CalendarDemo.this, eventTitle, Toast.LENGTH_LONG).show()
}
}else if(v == mWriteEventButton){
//获取要出入的gmail账户的id
String calId = ""
Cursor userCursor = getContentResolver().query(Uri.parse(calanderURL), null,
null, null, null)
if(userCursor.getCount() >0){
userCursor.moveToFirst()
calId = userCursor.getString(userCursor.getColumnIndex("_id"))
}
ContentValues event = new ContentValues()
event.put("title", "与苍井空小-姐动作交流")
event.put("description", "Frankie受空姐邀请,今天晚上10点以后将在Sheraton动作交流.lol~")
//插入hoohbood@gmail.com这个账户
event.put("calendar_id",calId)
Calendar mCalendar = Calendar.getInstance()
mCalendar.set(Calendar.HOUR_OF_DAY,10)
long start = mCalendar.getTime().getTime()
mCalendar.set(Calendar.HOUR_OF_DAY,11)
long end = mCalendar.getTime().getTime()
event.put("dtstart", start)
event.put("dtend", end)
event.put("hasAlarm",1)
Uri newEvent = getContentResolver().insert(Uri.parse(calanderEventURL), event)
long id = Long.parseLong( newEvent.getLastPathSegment() )
ContentValues values = new ContentValues()
values.put( "event_id", id )
//提前10分钟有提醒
values.put( "minutes", 10 )
getContentResolver().insert(Uri.parse(calanderRemiderURL), values)
Toast.makeText(CalendarDemo.this, "插入事件成功!!!", Toast.LENGTH_LONG).show()
}
}
}
第四步:在AndroidManifest.xml中申请权限,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutor.calendardemo"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".CalendarDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.READ_CALENDAR"/>
<uses-permission android:name="android.permission.WRITE_CALENDAR"/>
</manifest>
第五步:运行上述Android工程,查看效果:
微信小程序日历组件calendar详解及实例模版使用:
src="../cal/calendar.wxml"卜和>
is="calendar"
data="{{selected_value,days,month,years,lunar_years,lunar_month,lunar_days,selectDateType,l
unar_selected_value}}">
JS代码使用:
var
Calendar
=
require('../cal/calendar')
Page({
data:
{
selected_value:
[],
days:
[],
month:
[],
years:
[],
lunar_years:
[],
lunar_month:
[],
lunar_days:
[],
selectDateType:
1,
lunar_selected_value:
[]
},
....
//
指定型手盯选择器回调函数
new
Calendar('key',
this,
function(date){
that.setData({
date:
date
})
})
样式
.calendar{
position:
absolute
bottom:
0
width:
100%
z-index:
999
background-color:
#fff
}
.tab{
display:inline-block
width:50%
text-align:center
font-size:16px
color:
#ccc
}
.tab-bar{
background-color:
#eee
height:
40px
line-height:
40px
}
.tab-bar
.active{
color:
#000
}
.selected-item{
font-size:
28px
}
.event-type_parent{
font-size:
14px
}
.event-type_child{
text-align:
center
line-height:
30px
}
.event-type_txt{
display:
inline-block
}
以上用法看不懂的话,具体就参考代码里面index目录下。
感谢阅读,希望能帮助到大家,谢谢大薯纯家对本站的支持!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)