Giter Site home page Giter Site logo

gregorianlunarcalendar's Introduction

GregorianLunarCalendar

Android Arsenal

GregorianLunarCalendar提供了农历+公历的日期选择模式,同时支持公历+农历的无缝切换

控件截图

Example Image

效果图

Example Image

静态截图以及渐变效果

说明

上一个版本

上一个版本的GregorianLunarCalendar通过NumberPicker构造的日期选择器,可定制性较差,如:
1.每个NumberPicker只能显示3个选项;
2.setValue()以及滑动时时没有过渡动效;
3.滑动fling的速度较慢等等。

最新版本

前段时间抽空写了一个NumberPickerView控件,算是一个比原生的NumberPicker在使用上更加优美的控件,NumberPickerView的项目地址在此处: https://github.com/Carbs0126/NumberPickerView

此版本的GregorianLunarCalendar只是使用NumberPickerView替换NumberPicker,做成了一个更加优美的日期选择控件。 由于我将NumberPicker中常用的方法如setValue() setMinValue() setMaxValue() setDisplayedValues()等函数均在NumberPickerView中实现了,因此移植起来非常简单。 GregorianLunarCalendar控件效果如下:

具有的特性

UI显示效果的改进:

1.内容的重新设定时可以选择是否添加动效,即从公历转换到农历时,dayPicker肯定是需要重新设置value的,如果设置为添加动效,那么在切换时可以看到滚动效果;
2.在切换年/月时,如果月/日需要改变时(如每个月的天数不同),具有滚动效果,且滚动的路径是选择最短路径;

具有的功能:

1.公历年月日选择,年月改变时会联动改变相对应的月日时期显示,确保公历日期显示正确;
2.农历年月日选择,同样会有联动改变对应日期的效果,确保日期符合农历历法,包括闰月、大小月等;
3.公历农历互相转换时,实现无缝切换,如2016年2月29日切换为农历则显示二零一六年一月廿二日,农历转公历同样效果;
4.显示范围为1901年-2100年,满足大部分使用需求;
5.在1901年与2100年,确保公历农历转换时的边界限制。

使用方法:

1.xml中声明:

    <cn.carbs.android.gregorianlunarcalendar.library.view.GregorianLunarCalendarView
        android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:glcv_GregorianThemeColor="@color/colorGregorian"//公历主题颜色
        app:glcv_LunarThemeColor="@color/colorLunar"//农历主题颜色
        app:glcv_NormalTextColor="#FF777777"//文字颜色
        app:glcv_ScrollAnimation="true"/>//是否开启滚动动画效果,默认true

2.java代码中的使用:

  //找到View
  GregorianLunarCalendarView mGLCView = (GregorianLunarCalendarView) this.findViewById(R.id.calendar_view)
  
  //3种方式进行初始化:
  //1.默认日期今天,默认模式公历
  mGLCView.init();
  //2.指定日期,默认模式公历
  mGLCView.init(Calendar c);
  //3.指定日期,指定公历/农历模式。这里的公历模式/农历模式是指显示时采用的显示方式,
  //  和前面的参数Calendar无关,无论使用Calendar还是ChineseCalendar,GregorianLunarCalendarView需要的只是某一指定的年月日。
  mGLCView.init(Calendar c, boolean isGregorian);
  
  //获取数据:
  //采用GregorianLunarCalendarView.CalendarData内部类来存储当前日期,使用getCalendarData()函数获取选中date数据
  GregorianLunarCalendarView.CalendarData calendarData = mGLCView.getCalendarData();
  //进一步获取日期
  Calendar calendar = calendarData.getCalendar();//返回的是ChineseCalendar对象
  //若当前时间是: 公历2016年06月20日 农历二零一六年五月十六,那么获取的返回值如下:
  int yearG = calendar.get(Calendar.YEAR);//获取公历年 2016
  int monthG = calendar.get(Calendar.MONTH) + 1;//获取公历月 6
  int dayG = calendar.get(Calendar.DAY_OF_MONTH);//获取公历日 20
  int yearL = calendar.get(ChineseCalendar.CHINESE_YEAR);//获取农历年 2016
  int monthL = calendar.get(ChineseCalendar.CHINESE_MONTH));//获取农历月 5//注意,如果是闰五月则返回-5
  int dayL = calendar.get(ChineseCalendar.CHINESE_DATE);//获取农历日 20
  
  //添加日期改变的回调
  mGLCView.setOnDateChangedListener(new GregorianLunarCalendarView.OnDateChangedListener(){
        @Override
        public void onDateChanged(GregorianLunarCalendarView.CalendarData calendarData) {
            Calendar calendar = calendarData.getCalendar();
            String showToast = "Gregorian : " + calendar.get(Calendar.YEAR) + "-"
                         + (calendar.get(Calendar.MONTH) + 1) + "-"
                         + calendar.get(Calendar.DAY_OF_MONTH) + "\n"
                         + "Lunar     : " + calendar.get(ChineseCalendar.CHINESE_YEAR) + "-"
                         + (calendar.get(ChineseCalendar.CHINESE_MONTH)) + "-"
                         + calendar.get(ChineseCalendar.CHINESE_DATE);
            mChangedDate.setText(showToast);
           }
        }
    );

3.自定义属性说明:

    //自定义属性包括如下几种
    <declare-styleable name="GregorianLunarCalendarView">
        <attr name="glcv_ScrollAnimation" format="reference|boolean" />//切换公农历模式时,是否开启动画效果
        <attr name="glcv_GregorianThemeColor" format="reference|color" />//公历主题颜色
        <attr name="glcv_LunarThemeColor" format="reference|color" />//农历主题颜色
        <attr name="glcv_NormalTextColor" format="reference|color" />//普通状态下的文字颜色
    </declare-styleable>

注:
sample中的公农历切换控件使用的是IndicatorView,项目地址在此处: https://github.com/Carbs0126/IndicatorView

TODO

由于这种选择样式是滚动选择,因此,不支持设置日期选择区间。如果想要控制日期选择的区间,请使用DatePicker或其他第三方控件,当然我现在正打算写一个DatePicker控件。

License

Copyright 2016 Carbs.Wang (GregorianLunarCalendar)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

gregorianlunarcalendar's People

Contributors

bryant1410 avatar carbs0126 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gregorianlunarcalendar's Issues

ChineseCalendar.java problem

Hi, I got error in ChineseCalendar.java
at these lines:
public ChineseCalendar(boolean isChinese, int y, int m, int d) {
if (isChinese) {
set(CHINESE_YEAR, y);
set(CHINESE_MONTH, m);
set(CHINESE_DATE, d);
} else {
set(y, m, d);
}

My Android Studio kept saying CHINESE_YEAR, CHINESE_MONTH, CHINESE_DATE must be one of Calendar.ERA, DAY_OF_MONTH...
Thx.

是否可以帮我解决一下问题

我将用此控件放在了popWindow上,在Activity上点击按钮,弹出该控件,选择时间是没有问题的,但是如果不滑动,时间就是null,有没有办法解决?

编译出错

无法访问NumberPickerView
mGLCView.setOnDateChangedListener(new GregorianLunarCalendarView.OnDateChangedListener(){
^
找不到cn.carbswang.android.numberpickerview.library.NumberPickerView的类文件

java.lang.ClassCastException: java.util.GregorianCalendar cannot be cast to ChineseCalendar

    java.lang.ClassCastException: java.util.GregorianCalendar cannot be cast to com.pmm.remember.views.lunarview.ChineseCalendar
        at com.pmm.remember.views.lunarview.GregorianLunarCalendarView.checkCalendarAvailable(GregorianLunarCalendarView.java:499)
        at com.pmm.remember.views.lunarview.GregorianLunarCalendarView.setConfigs(GregorianLunarCalendarView.java:153)
        at com.pmm.remember.views.lunarview.GregorianLunarCalendarView.init(GregorianLunarCalendarView.java:146)

Question

is it lunar calendar as you know there is no concept of 31 Date in Lunar Calendar

意见

能给出一个移植到NumberPickerView版本的日历控件吗

how add value change click listener?

怎么添加日期改变监听事件,因为我要改变日期的同时,还要更新一个编辑框的日期值。
我没有发现别的函数只有一个
public void onValueChange(NumberPickerView picker, int oldVal, int newVal)
但是不明白为什么要传入NumberPickerView,且监听事件应该如何使用?

感谢

refreshByNewDisplayedValues()的问题

你好,我用了你的控件,来做省市地区三级联动,
滑动省,更新市区和省用的refreshByNewDisplayedValues(),
滑动市,更新省也是这个方法.
但是会报错:minShowIndex should be less than maxShowIndex,minShowIndex is 0,maxShowIndex is -1
请问要怎么解决啊

Create table Lunar

I want create a table lunar. When scroll table Gregorian then change table Lunar. But when i use code:

"GregorianLunarCalendarView.CalendarData calendarData =
mGLCView.getCalendarData();
Calendar calendars = calendarData.getCalendar();

    mGLCViewLunar.init(calendars,false);

"

Result in Lunar Table in fist open activity : 1901 - 01 - 01.

But when i scroll Gegorian table then result in Lunar Table is true.
Please help me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.