android自定义秒表,Android自定义Chronometer实现短信验证码秒表倒计时功能
本文實(shí)例為大家分享了Chronometer實(shí)現(xiàn)倒計(jì)時(shí)功能,Android提供了實(shí)現(xiàn)按照秒計(jì)時(shí)的API,供大家參考,具體內(nèi)容如下
一、自定義ChronometerView 繼續(xù)自TextView
主要原理:先設(shè)置一個(gè)基準(zhǔn)倒計(jì)時(shí)時(shí)間mBaseSeconds,內(nèi)置handler 每隔1s發(fā)送一個(gè)空消息,mRemainSeconds--,同時(shí)刷新界面視圖,回調(diào)給外部調(diào)用者,只到為零。外部調(diào)用者可通過start()/pause()/stop()來控制計(jì)時(shí)器的工作狀態(tài)。
可以app中發(fā)送短信驗(yàn)證碼的場(chǎng)景為例,做了一個(gè)很粗糙的界面,但功能都實(shí)現(xiàn)了。
/**
* @name 倒計(jì)時(shí)器(類似妙表倒數(shù)計(jì)時(shí),支持暫停、停止、重新開始)
* @author Fanjb
* @date 2015年11月6日
*/
public class ChronometerView extends TextView {
/**
* A callback that notifies when the chronometer has decremented on its own.
*
* @author Fanjb
*/
public interface OnTickChangeListener {
/**
* remain seconds changed
*
* @param view
* @param remainSeconds
*/
public void onTickChanged(ChronometerView view, long remainSeconds);
}
private long mBase;
private long mRemainSeconds;
private boolean mStarted;
private boolean mReStart;
private boolean mVisible;
private boolean mIsEnable;
private OnTickChangeListener mTickListener;
public ChronometerView(Context context) {
this(context, null);
}
public ChronometerView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public ChronometerView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
updateText(mRemainSeconds);
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
mVisible = visibility == VISIBLE;
updateStatus();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mVisible = false;
updateStatus();
}
/**
* 啟動(dòng)計(jì)時(shí)器
*/
public void start() {
if (mReStart && !mStarted) {
mRemainSeconds = mBase;
}
mStarted = true;
updateStatus();
}
/**
* 暫停計(jì)時(shí)器
*/
public void pause() {
if (mStarted) {
mStarted = mReStart = false;
updateStatus();
}
}
/**
* 停止計(jì)時(shí)器,再次調(diào)用 start()重新啟動(dòng)
*/
public void stop() {
mStarted = false;
mReStart = true;
updateStatus();
updateText(mRemainSeconds = 0);
dispatchTickListener();
}
/**
* 刷新內(nèi)部狀態(tài)
*/
private void updateStatus() {
boolean isEnable = mVisible && mStarted;
if (mIsEnable != isEnable) {
if (isEnable) {
mHandler.sendMessage(Message.obtain(mHandler, TICK_WHAT));
} else {
mHandler.removeMessages(TICK_WHAT);
}
mIsEnable = isEnable;
}
}
private static final int TICK_WHAT = 1;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (mRemainSeconds > 0) {
updateText(--mRemainSeconds);
dispatchTickListener();
sendMessageDelayed(Message.obtain(this, TICK_WHAT), 1000);
}
}
};
private void updateText(long now) {
String text = DateUtils.formatElapsedTime(now);
setText(text);
}
/**
* 在未啟動(dòng)狀態(tài)下設(shè)置開始倒計(jì)時(shí)時(shí)間
*
* @param baseSeconds
*/
public void setBaseSeconds(long baseSeconds) {
if (baseSeconds > 0 && baseSeconds != mBase && !mStarted) {
mBase = mRemainSeconds = baseSeconds;
updateText(mRemainSeconds);
}
}
/**
* 剩余時(shí)間
*
* @return
*/
public long getRemainSeconds() {
return mRemainSeconds;
}
public void setOnTickChangeListener(OnTickChangeListener listener) {
mTickListener = listener;
}
public OnTickChangeListener getTickListener() {
return mTickListener;
}
private void dispatchTickListener() {
if (mTickListener != null) {
mTickListener.onTickChanged(this, getRemainSeconds());
}
}
@Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(ChronometerView.class.getName());
}
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(Chronometer.class.getName());
}
}
二、xml 中沒有加入自定義的控件屬性,同TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:id="@+id/chronometer_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/chronometer_view_bg"
android:enabled="true"
android:text="00:00" />
android:id="@+id/start_chronometer_view_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Start" />
android:id="@+id/pause_chronometer_view_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Pause" />
android:id="@+id/stop_chronometer_view_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="Stop" />
三、在Activity中做一個(gè)簡(jiǎn)單的測(cè)試(可以發(fā)送短信驗(yàn)證碼的實(shí)際應(yīng)用場(chǎng)景為例)
public class ChronometerActivity extends Activity {
private ChronometerView mChronometerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clock);
// 自定義計(jì)時(shí)器
if (mChronometerView == null) {
mChronometerView = (ChronometerView) findViewById(R.id.chronometer_view);
mChronometerView.setBaseSeconds(60);
mChronometerView.setOnTickChangeListener(new OnTickChangeListener() {
@Override
public void onTickChanged(ChronometerView view, long curTimeMills) {
System.out.println(curTimeMills);
view.setEnabled(curTimeMills == 0 || curTimeMills == 60);
if (curTimeMills == 0) {
mChronometerView.setText("重新發(fā)送");
}
}
});
mChronometerView.setText("點(diǎn)擊發(fā)送驗(yàn)證碼");
}
findViewById(R.id.start_chronometer_view_btn).setOnClickListener(mClickListener);
findViewById(R.id.pause_chronometer_view_btn).setOnClickListener(mClickListener);
findViewById(R.id.stop_chronometer_view_btn).setOnClickListener(mClickListener);
}
private View.OnClickListener mClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_chronometer_view_btn:
if (mChronometerView != null) {
mChronometerView.start();
}
break;
case R.id.pause_chronometer_view_btn:
if (mChronometerView != null) {
mChronometerView.pause();
}
break;
case R.id.stop_chronometer_view_btn:
if (mChronometerView != null) {
mChronometerView.stop();
}
break;
}
}
};
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
總結(jié)
以上是生活随笔為你收集整理的android自定义秒表,Android自定义Chronometer实现短信验证码秒表倒计时功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [css3动画教程]:逐帧自适应精灵图
- 下一篇: 计算机编程怎样打符号,在CAD中如何输入