Git Product home page Git Product logo

datepicker's Introduction

Android 自定义日期选择控件

参考:https://github.com/liuwan1992/CustomDatePicker ,感谢! 在此基础上添加了,直接引入工程使用

如果想修改可以导入依赖datepickerlibrary工程源码进行修改

依赖

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
	        implementation 'com.github.kavin-tian:datepicker:1.1.0'
	}

使用

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView datetime = findViewById(R.id.datetime);
        Button pickTime = findViewById(R.id.pick_time);
        pickTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTimerPicker(datetime, "").show(System.currentTimeMillis());
            }
        });
    }

    /**
     * 初始化时间选择器
     */
    private CustomDatePicker getTimerPicker(TextView tv_time, String pattern) {
        String beginTime = "2018-10-01 00:00";
        String endTime = "2025-10-01 00:00";

        // 通过日期字符串初始化日期,格式请用:yyyy-MM-dd HH:mm
        CustomDatePicker mTimerPicker = new CustomDatePicker(this, new CustomDatePicker.Callback() {
            @Override
            public void onTimeSelected(long timestamp) {
                String datetime;
                if (TextUtils.isEmpty(pattern)) {
                    datetime = timeStamp2date(timestamp, pattern_default);
                } else {
                    datetime = timeStamp2date(timestamp, pattern);
                }
                tv_time.setText(datetime);

            }
        }, beginTime, endTime);
        // 允许点击屏幕或物理返回键关闭
        mTimerPicker.setCancelable(true);
        // 显示时和分
        mTimerPicker.setCanShowPreciseTime(true);
        // 允许循环滚动
        mTimerPicker.setScrollLoop(true);
        // 允许滚动动画
        mTimerPicker.setCanShowAnim(true);
        // 隐藏日期,仅显示时间
        //mTimerPicker.hideDate();

        return mTimerPicker;

    }

    public static final String pattern_default = "yyyy-MM-dd HH:mm:ss";

    /**
     * 时间戳转字符串
     */
    public static String timeStamp2date(long timestamp, String pattern) {
        return new SimpleDateFormat(pattern, Locale.CHINA).format(new Date(timestamp));
    }
}

datepicker's People

Contributors

kavin-tian avatar

Watchers

 avatar

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.