Created
April 3, 2016 11:20
-
-
Save Tikitoo/0391def959cda1792cfcd43258ca9b2c to your computer and use it in GitHub Desktop.
a Count Down View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.tikitoo.android.androiddemo; | |
import android.content.Context; | |
import android.os.CountDownTimer; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import android.widget.TextView; | |
/** | |
* Created by Tikitoo on 2016/4/3. | |
*/ | |
public class CountDownView extends TextView implements View.OnClickListener { | |
private MyCountTimer mMyCountTimer; | |
public CountDownView(Context context) { | |
this(context, null); | |
} | |
public CountDownView(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public CountDownView(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
setOnClickListener(this); | |
} | |
@Override | |
protected void onAttachedToWindow() { | |
super.onAttachedToWindow(); | |
} | |
boolean flag = true; | |
@Override | |
protected void onDetachedFromWindow() { | |
super.onDetachedFromWindow(); | |
if (mMyCountTimer != null) { | |
mMyCountTimer.cancel(); | |
} | |
} | |
@Override | |
public void onClick(View v) { | |
if (mMyCountTimer == null) { | |
mMyCountTimer = new MyCountTimer(10 * 1000, 1000); | |
} | |
if (flag) { | |
mMyCountTimer.start(); | |
onStart(); | |
} | |
} | |
class MyCountTimer extends CountDownTimer { | |
public MyCountTimer(long millisInFuture, long countDownInterval) { | |
super(millisInFuture, countDownInterval); | |
} | |
@Override | |
public void onTick(long s) { | |
flag = false; | |
setText(s / 1000 + "秒"); | |
} | |
@Override | |
public void onFinish() { | |
flag = true; | |
setText("重新发送"); | |
CountDownView.this.onPost(); | |
} | |
} | |
private Callback mCallback; | |
public void setCallback(Callback callback) { | |
mCallback = callback; | |
} | |
public void onStart() { | |
if (mCallback != null) { | |
mCallback.onPre(); | |
} | |
} | |
public void onPost() { | |
if (mCallback != null) { | |
mCallback.onPost(); | |
} | |
} | |
public interface Callback { | |
void onPre(); | |
void onPost(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.tikitoo.android.androiddemo; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity implements CountDownView.Callback { | |
private CountDownView mCountDownView; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mCountDownView = (CountDownView) findViewById(R.id.count_view); | |
mCountDownView.setCallback(this); | |
} | |
@Override | |
public void onPre() { | |
mCountDownView.setTextColor(R.color.text_click); | |
mCountDownView.setBackgroundResource(R.color.back_click); | |
} | |
@Override | |
public void onPost() { | |
mCountDownView.setTextColor(R.color.text_already); | |
mCountDownView.setBackgroundResource(R.color.back_already); | |
Toast.makeText(MainActivity.this, "onFinish", Toast.LENGTH_SHORT).show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
CountDownView 是一个自定义的验证码定时器View,它会自动倒计时,并且提供了onPre() 和onPost() 方法,来发送API,和验证码发送结束。下面是预览图
![](https://camo.githubusercontent.com/08f920b97034825eef380882949aa0b79bbe411e40307e73525c12771457959c/687474703a2f2f7777312e73696e61696d672e636e2f6c617267652f363836323233373767773166326a73306f7672673967323038773034627134662e676966)