Android,androidsdk

來源:互聯網
上載者:User

Android,androidsdk

CountDownTimer                                                                     

CountDownTimer這個類,實現了倒計時的功能。將後台線程的建立和Handler隊列封裝成一個方便的類調用。

這個類比較簡單,只有四個方法:onTick,onFinsh、cancel和start。其中前面兩個是抽象方法,所以要重寫一下。

下面是官方給的一個小例子:

new CountdownTimer(30000, 1000) {      public void onTick(long millisUntilFinished) {          mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);      }      public void onFinish() {          mTextField.setText("done!");      }   }.start();

Code                                                                                   

package com.yydcdut.daojishi;import android.os.Bundle;import android.os.CountDownTimer;import android.app.Activity;import android.view.Menu;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {    private MyCount mc;      private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);tv = (TextView)findViewById(R.id.show);          mc = new MyCount(30000, 1000);          mc.start();    }    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }        /*定義一個倒計時的內部類*/      class MyCount extends CountDownTimer {             public MyCount(long millisInFuture, long countDownInterval) {                 super(millisInFuture, countDownInterval);             }             @Override             public void onFinish() {                 tv.setText("finish");                }             @Override             public void onTick(long millisUntilFinished) {                 tv.setText("請等待30秒(" + millisUntilFinished / 1000 + ")...");                 Toast.makeText(MainActivity.this, millisUntilFinished / 1000 + "", Toast.LENGTH_LONG).show();//toast有顯示時間延遲               }        }     }

主要是重寫onTick和onFinsh這兩個方法,onFinish()中的代碼是計時器結束的時候要做的事情;onTick(Long m)中的代碼是你倒計時開始時要做的事情,參數m是直到完成的時間,構造方法MyCount()中的兩個參數中,前者是倒計的時間數,後者是倒計每秒中間的間隔時間,都是以毫秒為單位。例如要倒計時30秒,每秒中間間隔時間是1秒,兩個參數可以這樣寫MyCount(30000,1000)。 將後台線程的建立和Handler隊列封裝成為了一個方便的類調用。

當你想取消的時候使用mc.cancel()方法就行了。

我是天王蓋地虎的分割線                                                             

原始碼:http://pan.baidu.com/s/1dD1Qx01

倒計時.zip


android是什

android就是機器人的意思 android手機就是使用安卓系統的手機,由Google開發出來的 和一般智能手機差不多 不知道你說的普通手機是什麼意思 android手機能夠多
 
android是什

android就是機器人的意思 android手機就是使用安卓系統的手機,由Google開發出來的 和一般智能手機差不多 不知道你說的普通手機是什麼意思 android手機能夠多
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.