码迷,mamicode.com
首页 > 移动开发 > 详细

Android---25---Chronometer计时器

时间:2015-04-21 18:08:40      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:android开发   chronometer   

Chronometer 计时器
 它提供了一个android:format属性,用于指定计时器的计时格式
  setBase (long base):设置起始时间
  setFormat (String format):设置显示时间的格式
  start():开始计时
  stop ():停止计时

  setOnchronometerTickListener():为计时器绑定时间监听器,当计时器改变时触发该监听器


计时20s,当时间到了则停止计时。

MainActivity.java:


import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;;

public class MainActivity extends Activity {

	private Chronometer ch;
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ch = (Chronometer) findViewById(R.id.test);
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//设置开始时间
				ch.setBase(SystemClock.elapsedRealtime());
				//开始计时
				ch.start();
				//按钮不可用
				button.setEnabled(false);
			}
		});
		ch.setOnChronometerTickListener(new OnChronometerTickListener() {
			
			@Override
			public void onChronometerTick(Chronometer chronometer) {
				// TODO Auto-generated method stub
				//如果计时超过20s
				if (SystemClock.elapsedRealtime() - ch.getBase() > 20*1000){
					//停止计时
					ch.stop();
					//按钮可用
					button.setEnabled(true);
				}
			}
		});
	}
}


activity_main.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.chronometerdemo2.MainActivity" >

    <Chronometer
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="启动" />

</LinearLayout>


用户自定义计时时间:


MainActivity.java:


import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;

public class MainActivity extends Activity implements OnClickListener {

	private Chronometer ch;
	private Button start, stop;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ch = (Chronometer) findViewById(R.id.test);
		start = (Button) findViewById(R.id.start);
		stop = (Button) findViewById(R.id.stop);
		start.setOnClickListener(this);
		stop.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.start:
			ch.setBase(SystemClock.elapsedRealtime());
			ch.start();
			start.setEnabled(false);
			break;
		case R.id.stop:
			ch.stop();
			start.setEnabled(true);
			break;
		default:
			break;
		}
	}
}


activity_main.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.chronometerdemo.MainActivity" >

    <Chronometer
        android:id="@+id/test"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="启动" />

        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="停止" />
    </LinearLayout>

</LinearLayout>



Android---25---Chronometer计时器

标签:android开发   chronometer   

原文地址:http://blog.csdn.net/u013476556/article/details/45172617

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!