The seventh bullet in Android development: simple clock (stopwatch)

Source: Internet
Author: User

The seventh bullet in Android development: simple clock (stopwatch)

This article undertakes, Android Development Fifth play: simple clock (alarm clock) and Android Development Sixth play: simple clock (timer), this part is about the stopwatch.

Layout

You can also create a new class (StopWatchView), extend it from LinearLayout, and use it for layout.


      
       
        
         
       
        
         
          
          
            < / LinearLayout> 
           
             < / ListView> 
            
             
             
             
             
            
           
          
         
        
       
      
     
    
   
      < / LinearLayout>
StopWatchView

These balabala items should also be defined at the beginning:

    private int tenMSecs = 0;    private Timer timer = new Timer();    private TimerTask timerTask = null;    private TimerTask showTimeTask = null;    private TextView tvHour, tvMinute, tvSecond, tvMSecond;    private Button btnSWStart, btnSWResume, btnSWReset, btnSWPause, btnSWRecord;    private ListView lvWatchTimeList;    private ArrayAdapter
  
    adapter;    private static final int MSG_WHAT_SHOW_TIME = 1;    public StopWatchView(Context context) {        super(context);    }    public StopWatchView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public StopWatchView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }
  

Looking at these comments, I found that there is no difference with the previous two articles. I will not talk nonsense about the Code directly.

@ Override protected void onFinishInflate () {super. onFinishInflate (); // set each corresponding TextView control to 0 tvHour = (TextView) findViewById (R. id. tvHour); tvHour. setText (0); tvMinute = (TextView) findViewById (R. id. tvMinute); tvMinute. setText (0); tvSecond = (TextView) findViewById (R. id. tvSecond); tvSecond. setText (0); tvMSecond = (TextView) findViewById (R. id. tvMSceond); tvMSecond. setText (0); // set the listening event btnSWRecord = (Button) findViewById (R. id. btnSWRecord); btnSWRecord. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {adapter. insert (String. format (% d: % d. % d, tenMSecs/100/60/60, tenMSecs/100/60% 60, tenMSecs/100% 60, tenMSecs % 100), 0) ;}}); btnSWPause = (Button) findViewById (R. id. btnSWPause); btnSWPause. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// stop stopTimer (); btnSWPause. setVisibility (View. GONE); btnSWResume. setVisibility (View. VISIBLE); btnSWReset. setVisibility (View. VISIBLE); btnSWRecord. setVisibility (View. GONE) ;}}); btnSWReset = (Button) findViewById (R. id. btnSWReset); btnSWReset. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// stop stopTimer (); tenMSecs = 0; adapter. clear (); btnSWStart. setVisibility (View. VISIBLE); btnSWPause. setVisibility (View. GONE); btnSWReset. setVisibility (View. GONE); btnSWRecord. setVisibility (View. GONE); btnSWResume. setVisibility (View. GONE) ;}}); btnSWResume = (Button) findViewById (R. id. btnSWResume); btnSWResume. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// start startTimer (); btnSWResume. setVisibility (View. GONE); btnSWReset. setVisibility (View. GONE); btnSWRecord. setVisibility (View. VISIBLE); btnSWPause. setVisibility (View. VISIBLE) ;}}); btnSWStart = (Button) findViewById (R. id. btnSWStart); btnSWStart. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {// start startTimer (); btnSWStart. setVisibility (View. GONE); btnSWPause. setVisibility (View. VISIBLE); btnSWRecord. setVisibility (View. VISIBLE) ;}}); btnSWRecord. setVisibility (View. GONE); btnSWPause. setVisibility (View. GONE); btnSWReset. setVisibility (View. GONE); btnSWResume. setVisibility (View. GONE); // Add the adapter to the list lvWatchTimeList = (ListView) findViewById (R. id. lvWatchTimeList); adapter = new ArrayAdapter
  
   
(GetContext (), android. r. layout. simple_list_item_1); lvWatchTimeList. setAdapter (adapter); // send the message showTimeTask = new TimerTask () {@ Override public void run () {Handler. sendEmptyMessage (MSG_WHAT_SHOW_TIME) ;}}; // start timing timer. schedule (showTimeTask, 200,200);} // start private void startTimer () {if (timerTask = null) {timerTask = new TimerTask () {@ Override public void run () {tenMSecs ++ ;}}; Timer. schedule (timerTask, 10, 10) ;}// end private void stopTimer () {if (timerTask! = Null) {timerTask. cancel (); timerTask = null ;}// cancel timing public void onDestory () {timer. cancel ();} private Handler handler = new Handler () {public void handleMessage (Message msg) {switch (msg. what) {// if the message matches, the corresponding time is calculated and displayed on the corresponding TextView. case MSG_WHAT_SHOW_TIME: tvHour. setText (tenMSecs/100/60/60/60 +); tvMinute. setText (tenMSecs/100/60% 60 +); tvSecond. setText (tenMSecs/100% 60 +); tvMSecond. setText (tenMSecs % 100 +); break; default: break ;}};};
  
End

Okay, this is the end.

Similarly, if you need code, you can leave a comment to your mailbox. The code will be updated, and the comments will be updated ......

 

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.