首页 > 代码库 > 简易计时器
简易计时器
这个简易计时器,实现了简单的计时,点击开始按钮开始计时,点击停止按钮就停止,屏幕现实开始到停止的时间差。在run()方法里已换算成时分秒。
代码仅供参考:
strings.xml
<string name="app_name">计时器</string> <string name="text_time">00:00</string> <string name="button_start">记录</string> <string name="button_stop">取消</string>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <TextView android:id="@+id/time" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="30sp" android:text="@string/text_time" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_margin="20dp" android:orientation="horizontal" > <Button android:id="@+id/button_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_gravity="top|right" android:text="@string/button_start" /> <Button android:id="@+id/button_stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_gravity="top|right" android:text="@string/button_stop" /> </RelativeLayout> </RelativeLayout>
MainActivity.java
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; /** * 计时器 * @author JianDan */ public class MainActivity extends Activity implements OnClickListener { private TextView timer_time; private int count = 0; private int s = 0; private int m = 0; private int h = 0; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); timer_time = (TextView) findViewById(R.id.time); // handler.postDelayed(runnable, 1000); this.findViewById(R.id.button_start).setOnClickListener(this); this.findViewById(R.id.button_stop).setOnClickListener(this); } private Handler handler = new Handler(); private Runnable runnable = new Runnable() { @Override public void run() { String time = ""; count++; if (count < 60) { // 小于60s if (count < 10) { time = "00:0" + count; } else { time = "00:" + count; } } else { // 大于等于60s m = count / 60; // 取整数,分钟 s = count % 60; // 取余数,秒数 if (m < 10) { // 小于10分钟 if (s < 10) { time = "0" + m + ":0" + s; } else { time = "0" + m + ":" + s; } } else { if (s < 10) { time = m + ":0" + s; } else { time = m + ":" + s; } } if (m > 60) { h = m / 60; time = h + ":" + m + ":" + s; } } timer_time.setText(time); handler.postDelayed(this, 1000); } }; @Override public void onClick(View v) { switch (v.getId()) { case R.id.button_start: handler.postDelayed(runnable, 1000); break; case R.id.button_stop: count = 0; handler.removeCallbacks(runnable); break; default: break; } } }
参考博客:
http://blog.csdn.net/ithomer/article/details/6903084
http://blog.csdn.net/m_changgong/article/details/6844441
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。