Code Monkey home page Code Monkey logo

countdownbutton's Introduction

#CountdownButton

An Android library, provides a Button with a countdown timer.

##Samples

sample1

sample2

device-2016-06-05-135126.png

##Integration

Add the following line to the dependencies section of build.gradle.

compile 'mlxy.countdownbutton:countdownbutton:1.6'

##Use XML Attributes

In the root element of your layout file (like activity_main.xml), declare the namespace:

<XXXLayout
    ......
    xmlns:app="http://schemas.android.com/apk/res-auto" <!-- this line -->
    ......>

Here are several attributes supported in current version:

<mlxy.countdownbutton.CountdownButton
    app:countdown="10"              		<!-- total countdown time -->
    app:disableOnCountdown="true"   		<!-- if the button will be disabled when countdown starts -->
    app:interval="1"                		<!-- interval between CountdownButton updates its text -->
    app:prefix="Retry in "          		<!-- prefix of the button text when counting down -->
    app:startOnClick="true"         		<!-- if countdown starts right after button clicked -->
    app:suffix=" second(s)"         		<!-- suffix of the button text when counting down -->
	app:keepCountingDownInBackground="true" <!-- to avoid sms bomb etc. -->
	app:identifier="button_register"        <!-- for saving background countdown status separately, see issue #8 -->
    app:timeUnit="second" />        		<!-- yes, time unit -->

##In code

Instantiate a instance by findViewById() or Constructor and configure with these APIs:

countdownButton.setPrefix("I'm prefix");
countdownButton.setSuffix("I'm suffix");
countdownButton.setTimeUnit(CountdownButton.TIMEUNIT_MILLISECOND);
countdownButton.setCountdown(10 * 1000);
countdownButton.setInterval(1);
countdownButton.setStartOnClick(true);
countdownButton.setDisableOnCountdown(true);
countdownButton.setIdentifier("button_forgot_password")

CountdownButton is also compatible with Button's methods like:

countdownButton.setText("Click me to count down");

Also, if you set CountdownButton.setStartOnClick() with false, you can start it when you want by:

countdownButton.startCountdown();

Or cancel it at any time by:

countdownButton.cancelCountdown();

*Only use CountdownButton.setDisableOnCountdown(boolean) when you really need it, it still have some glitches that I can't deal with.

*Try not to mess with View.setEnabled(boolean), I don't know what will happen.

##Custom Countdown Text Provider

        countdownButton.setCountdownProvider(new CountdownButton.IProvider() {
            @NonNull
            @Override
            public CharSequence getCountdownText(long millisUntilFinished, int timeUnit) {
                // custom code
            }
        });

###For further details, see app module.

##Author

mlxy

##License

The MIT License (MIT)

countdownbutton's People

Contributors

chihane avatar

Stargazers

kaierwen avatar  avatar Saxxhw avatar cocoa avatar Stark.Tony avatar fanhl avatar  avatar  avatar  avatar

Watchers

 avatar

countdownbutton's Issues

Custom countdown text Provider.

hello!

any way to add a countdown text provider like this:
可以自定义倒计时的内容吗?

countdownButton.setCountdownTextProvider(new CountdownTextProvider(){
    @override
    public CharSequence provideCountdown(int countdown){
        //custom code.
    }
});

app:keepCountingDownInBackground设置为true后导致多个按钮倒计时错误

您好,在使用过程中发现了一个小问题,因为需求导致项目中有多处需要使用该控件,设置app:keepCountingDownInBackground为true后,发现第一个按钮的倒计时成功,跳转第二个界面之后,该界面中的CountdownButton的倒计时是与前一个Button相同的,原因是该控件在整个应用只保存了一个数值在Sp里,项目里的全部CountdownButton按钮都回去设置app:keepCountingDownInBackground为true后都回去读取这个值改变当前倒计时。希望大神可以继续优化一下

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.