Android Custom Chronometer realizes the short message verification Code stopwatch countdown function _android

Source: Internet
Author: User
Tags visibility

This example for you to share the chronometer countdown function, Android provides the implementation of the second timer API for your reference, the specific content as follows

First, custom Chronometerview continue to TextView

main principle: first set a benchmark countdown time mbaseseconds, built-in handler every 1s send an empty message, mremainseconds--, while refreshing the interface view, callback to the external caller, only to zero. The external caller can control the working state of the timer through the start ()/pause ()/stop ().
The app can send a text message verification code scenario For example, made a very rough interface, but the function is realized.

/** * @name countdown timer (similar to the countdown clock, support pause, stop, restart) * @author FANJB * @date November 6, 2015/public class Chronometerview Exten 
 DS TextView {/** * A callback that notifies then Chronometer has decremented on its own. * * @author FANJB/public interface Ontickchangelistener {/** * remain seconds changed * * @param VI 
 EW * @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 {This (context, NULL); 
 Public Chronometerview (context, AttributeSet attrs) {Super (context, attrs, 0); Chronometerview (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 (); 
 /** * Start Timer */public void start () {if (Mrestart &&!mstarted) {mremainseconds = Mbase; 
 } mstarted = true; 
 UpdateStatus (); 
  /** * Pause Timer */public void pause () {if (mstarted) {mstarted = Mrestart = false; 
 UpdateStatus (); 
 }/** * Stop timer, call Start again () reboot */public void Stop () {mstarted = false; 
 Mrestart = true; 
 UpdateStatus (); 
 UPDATETEXT (mremainseconds = 0); 
 Dispatchticklistener (); 
 /** * Refresh Internal state */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); /** * Set start countdown time in the not-started state * * @param baseseconds/public void setbaseseconds (long baseseconds) {if base 
  Seconds > 0 && baseseconds!= mbase &&!mstarted) {mbase = Mremainseconds = Baseseconds; 
 UPDATETEXT (Mremainseconds); 
 }/** * The remaining time * * @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, getremains 
 Econds ()); } @Override public void Oninitializeaccessibilityevent (Accessibilityevent event) {Super.oninitializeaccessibi 
 Lityevent (event); 
 Event.setclassname (ChronometerView.class.getName ()); @Override public void Oninitializeaccessibilitynodeinfo (Accessibilitynodeinfo info) {Super.oninitializeaccessib 
 Ilitynodeinfo (info); 
 Info.setclassname (Chronometer.class.getName ()); } 
}

  Two, no custom control properties are added to the XML, same TextView

<linearlayout android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Horizontal "> <com.freedoman.widgets.calendar.chronometerview android:id=" @+id/chronometer_view "android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "5DP" Android:backgroun d= "@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:l" ayout_marginleft= "5DP" android:text= "Start"/> <button android:id= "@+id/pause_chronometer_view_btn" and Roid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "5DP" Android:tex t= "Pause"/> <button android:id= "@+id/stop_chronometer_view_btn" android:layout_width= "Wrap_content" an Droid:layout_height= "Wrap_coNtent "android:layout_marginleft=" 5DP "android:text=" Stop "/> </LinearLayout>  

Third, make a simple test in the activity (for example, an actual scenario where SMS authentication codes can be sent)

public class Chronometeractivity extends activity {private Chronometerview Mchronometerview; 
 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
 Setcontentview (R.layout.activity_clock); 
  Custom Timer if (Mchronometerview = = null) {Mchronometerview = (Chronometerview) Findviewbyid (R.id.chronometer_view); 
  Mchronometerview.setbaseseconds (60); Mchronometerview.setontickchangelistener (New Ontickchangelistener () {@Override public void ontickchanged (chronomete 
   Rview view, Long Curtimemills) {System.out.println (curtimemills); 
   view.setenabled (Curtimemills = = 0 | | | curtimemills = 60); 
   if (Curtimemills = = 0) {mchronometerview.settext ("resend"); 
  } 
  } 
  }); 
 Mchronometerview.settext ("Click to send Verification Code"); 
 } 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; 
} 
 } 
 };  }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.