예배 규칙서 찾다
android Manifest Manifest.permission Manifest.permission_group android.accessibilityservice AccessibilityService android.accounts Account android.app NotificationManager android.bluetooth BluetoothAdapter BluetoothClass BluetoothClass.Device BluetoothClass.Device.Major BluetoothClass.Service BluetoothDevice BluetoothServerSocket BluetoothSocket android.content SharedPreferences android.database.sqlite SQLiteCursorDriver SQLiteOpenHelper android.graphics Bitmap android.location LocationListener Geocoder GpsStatus GpsStatus.Listener GpsStatus.NmeaListener GpsSatellite android.media AudioFormat AsyncPlayer AudioRecord AudioRecord.OnRecordPositionUpdateListener ThumbnailUtils AudioManager android.net TrafficStats MailTo LocalSocket android.os AsyncTask AsyncTask.Status CountDownTimer Message MessageQueue HandlerThread android.text Html android.util JsonWriter android.view ContextMenu ContextMenu.ContextMenuInfo Display ViewManager View ViewStub ViewTreeObserver ViewParent WindowManager GestureDetector Gravity MenuInflater ScaleGestureDetector SoundEffectConstants android.view.inputmethod InputConnection InputMethod InputMethodSession BaseInputConnection InputMethodManager android.widget AbsListView AbsListView.LayoutParams AbsListView.OnScrollListener AbsListView.RecyclerListener AbsoluteLayout AbsoluteLayout.LayoutParams AbsSeekBar AbsSpinner AdapterView AdapterView.AdapterContextMenuInfo AdapterView.OnItemLongClickListener AdapterView.OnItemSelectedListener AdapterView.OnItemClickListener AnalogClock BaseAdapter BaseExpandableListAdapter Button CheckBox CheckedTextView Checkable Chronometer Chronometer.OnChronometerTickListener CompoundButton CompoundButton.OnCheckedChangeListener CursorAdapter CursorTreeAdapter DatePicker DatePicker.OnDateChangedListener DialerFilter DigitalClock EditText Filter Filter.FilterListener Filter.FilterResults ExpandableListAdapter ExpandableListView.OnChildClickListener ExpandableListView.OnGroupClickListener ExpandableListView.OnGroupCollapseListener ExpandableListView.OnGroupExpandListener Filterable Gallery Gallery.LayoutParams GridView GridLayout GridLayout.Alignment RadioGroup ImageView ImageView.ScaleType HorizontalScrollView ImageButton ImageSwitcher FilterQueryProvider ListAdapter ListView MediaController MultiAutoCompleteTextView MultiAutoCompleteTextView.CommaTokenizer MultiAutoCompleteTextView.Tokenizer QuickContactBadge RadioButton RatingBar RatingBar.OnRatingBarChangeListener RelativeLayout RemoteViews ResourceCursorAdapter ResourceCursorTreeAdapter Scroller ScrollView SearchView SearchView.OnCloseListener SearchView.OnQueryTextListener SearchView.OnSuggestionListener SeekBar SeekBar.OnSeekBarChangeListener SimpleAdapter SimpleAdapter.ViewBinder SimpleCursorAdapter SimpleCursorAdapter.CursorToStringConverter SimpleCursorAdapter.ViewBinder SimpleCursorTreeAdapter SimpleCursorTreeAdapter.ViewBinder SimpleExpandableListAdapter SlidingDrawer SlidingDrawer.OnDrawerCloseListener SlidingDrawer.OnDrawerOpenListener SlidingDrawer.OnDrawerScrollListener Spinner SpinnerAdapter WrapperListAdapter TabHost TabHost.TabSpec TextView TimePicker TimePicker.OnTimeChangedListener Toast TableLayout TableLayout.LayoutParams TableRow TableRow.LayoutParams TabWidget TextSwitcher ToggleButton TwoLineListItem VideoView ViewAnimator ViewFlipper ViewSwitcher ViewSwitcher.ViewFactory ZoomButtonsController ZoomButtonsController.OnZoomListener ZoomButton ZoomControls dalvik.system DexFile
문자


CountDownTimer

版本:Android 4.0 r1

 

结构

继承关系

public abstract class CountDownTimer extends Object

        

java.lang.Object

android.os.CountDownTimer

 

类概述

定时执行在一段时候后停止的倒计时,在倒计时执行过程中会在固定间隔时间得到通知(译者:触发onTick方法),下面的例子显示在一个文本框中显示一个30s倒计时:

 new CountdownTimer(30000, 1000) {

     public void onTick(long millisUntilFinished) {

         mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);

     }

     public void onFinish() {

         mTextField.setText("done!");

     }

  }.start(); 

onTick的调用是同步的,保证这次调用不会在之前调用完成前发生。这里的同步机制主要是用来:onTick的实现需要很多时间执行比倒计时间隔更重要的事情。

 

构造函数

         public CountDownTimer (long millisInFuture, long countDownInterval)

参数

                   millisInFuture  从开始调用start()到倒计时完成并onFinish()方法被调用的毫秒数。(译者注:倒计时时间,单位毫秒)

                   countDownInterval   接收onTick(long)回调的间隔时间。(译者注:单位毫秒)

 

公共方法

public final void cancel ()

         取消倒计时(译者:取消后,再次启动会重新开始倒计时)        

                  

public abstract void onFinish ()

         倒计时完成时被调用    

 

public abstract void onTick (long millisUntilFinished)

         固定间隔被调用

参数

                            millisUntilFinished   倒计时剩余时间。

 

public synchronized final CountDownTimer start ()

         启动倒计时

 

补充

文章精选

Android 定时器

android倒计时功能的实现(CountDownTimer

示例代码

         Java

package com.test.countdowntimer;

 

import android.app.Activity;

import android.os.Bundle;

import android.os.CountDownTimer;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

 

import com.test.R;

 

public class CountDownTimeActivity extends Activity implements OnClickListener {

 

    TextView mTextView;

    Button mButton1;

    Button mButton2;

   

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

      

       setContentView(R.layout.countdown);

      

        mTextView = (TextView)findViewById(R.id.textView1);

       mButton1 = (Button)findViewById(R.id.button1);

       mButton2 = (Button)findViewById(R.id.button2);

       mButton1.setOnClickListener(this);

       mButton2.setOnClickListener(this);

    }

 

    CountDownTimer timer = new CountDownTimer(40000,1000) {

      

       @Override

       public void onTick(long millisUntilFinished) {

           mTextView.setText("seconds remaining: " + millisUntilFinished / 1000);

           try {

              Thread.sleep(1200);

           } catch (InterruptedException e) {

              e.printStackTrace();

           }

           Log.e("CountDown",millisUntilFinished+"");

       }

      

       @Override

       public void onFinish() {

           mTextView.setText("done");

       }

    };

   

    @Override

    public void onClick(View v) {

       switch(v.getId()){

       case R.id.button1:

           timer.start();

           break;

       case R.id.button2:

           timer.cancel();

           break;

       }

      

    }

}

 

 

         XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

  xmlns:android="http://schemas.android.com/apk/res/android"

  android:orientation="vertical"

  android:layout_width="match_parent"

  android:layout_height="match_parent">

    <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

    <Button android:text="开始" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

    <Button android:text="取消" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

   

</LinearLayout>

 

说明:

CountDownTimer的间隔为1s,我们在onTIck方法里面休眠了1.2s,所以log出来发现打印间隔变成了2s,即中间一次onTick方法没有被执行(不会在之前一次调用完成前被调用)。

 


이전 기사: 다음 기사: