国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義Chronometer實現短信驗證碼秒表倒計時功能

2020-02-21 17:29:46
字體:
來源:轉載
供稿:網友

當手機應用程序安裝后初始化時,為了提高用戶體驗,通常會以倒計時的形式給用戶一定的提示,本文是武林技術頻道小編介紹的Android自定義Chronometer實現短信驗證碼秒表倒計時功能,一起來看看吧!

一、自定義ChronometerView 繼續自TextView

主要原理:先設置一個基準倒計時時間mBaseSeconds,內置handler 每隔1s發送一個空消息,mRemainSeconds--,同時刷新界面視圖,回調給外部調用者,只到為零。外部調用者可通過start()/pause()/stop()來控制計時器的工作狀態。
可以app中發送短信驗證碼的場景為例,做了一個很粗糙的界面,但功能都實現了。

/**  * @name 倒計時器(類似妙表倒數計時,支持暫停、停止、重新開始)  * @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();  }   /**  * 啟動計時器  */  public void start() {  if (mReStart && !mStarted) {   mRemainSeconds = mBase;  }  mStarted = true;  updateStatus();  }   /**  * 暫停計時器  */  public void pause() {  if (mStarted) {   mStarted = mReStart = false;   updateStatus();  }  }   /**  * 停止計時器,再次調用 start()重新啟動  */  public void stop() {  mStarted = false;  mReStart = true;  updateStatus();  updateText(mRemainSeconds = 0);  dispatchTickListener();  }   /**  * 刷新內部狀態  */  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);  }   /**  * 在未啟動狀態下設置開始倒計時時間  *  * @param baseSeconds  */  public void setBaseSeconds(long baseSeconds) {  if (baseSeconds > 0 && baseSeconds != mBase && !mStarted) {   mBase = mRemainSeconds = baseSeconds;   updateText(mRemainSeconds);  }  }   /**  * 剩余時間  *  * @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

<LinearLayout  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:orientation="horizontal" >   <com.freedoman.widgets.calendar.ChronometerView   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" />   <Button   android:id="@+id/start_chronometer_view_btn"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_marginLeft="5dp"   android:text="Start" />   <Button   android:id="@+id/pause_chronometer_view_btn"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_marginLeft="5dp"   android:text="Pause" />   <Button   android:id="@+id/stop_chronometer_view_btn"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:layout_marginLeft="5dp"   android:text="Stop" />  </LinearLayout> 

三、在Activity中做一個簡單的測試(可以發送短信驗證碼的實際應用場景為例)

public class ChronometerActivity extends Activity {   private ChronometerView mChronometerView;   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_clock);   // 自定義計時器  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("重新發送");    }   }   });   mChronometerView.setText("點擊發送驗證碼");  }  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;   }  }  }; } 

上文是武林技術頻道小編介紹的Android自定義Chronometer實現短信驗證碼秒表倒計時功能的全部內容,大家學習到了多少呢?武林技術頻道小編每天都會為大家推薦值得大家學習的知識,繼續關注我們吧!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 勃利县| 通许县| 云和县| 抚宁县| 福建省| 图们市| 闽清县| 和平县| 泸水县| 镇雄县| 临桂县| 化州市| 安康市| 南澳县| 遂川县| 尚志市| 灵台县| 五峰| 阿拉善右旗| 闸北区| 广元市| 克东县| 大埔区| 开化县| 巴塘县| 桑日县| 广宗县| 平阳县| 石台县| 宜兴市| 珲春市| 通渭县| 常宁市| 沭阳县| 隆化县| 宁明县| 宾阳县| 清新县| 威远县| 甘泉县| 曲麻莱县|