android自定义秒表,Android实现的秒表计时器示例

本文实例讲述了Android实现的秒表计时器。分享给大家供大家参考,具体如下:

package com.liu.time;

import java.util.Timer;

import java.util.TimerTask;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.Context;

import android.content.DialogInterface;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.util.Log;

import android.view.KeyEvent;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.widget.ImageButton;

import android.widget.TextView;

public class MyTime extends Activity {

private long mlCount = 0;

private long mlTimerUnit = 100;

private TextView tvTime;

private ImageButton btnStartPause;

private ImageButton btnStop;

private Timer timer = null;

private TimerTask task = null;

private Handler handler = null;

private Message msg = null;

private boolean bIsRunningFlg = false;

private static final String MYTIMER_TAG = "MYTIMER_LOG";

// menu item

private static final int SETTING_TIMER_UNIT_ID = Menu.FIRST;

private static final int ABOUT_ID = Menu.FIRST + 1;

private static final int EXIT_ID = Menu.FIRST + 2;

private static final int SETTING_SECOND_ID = Menu.FIRST + 101;

private static final int SETTING_100MILLISECOND_ID = Menu.FIRST + 102;

// Setting timer unit flag

private int settingTimerUnitFlg = SETTING_100MILLISECOND_ID;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

tvTime = (TextView) findViewById(R.id.tvTime);

btnStartPause = (ImageButton) findViewById(R.id.btnStartPaunse);

btnStop = (ImageButton) findViewById(R.id.btnStop);

SharedPreferences sharedPreferences = getSharedPreferences(

&#

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java实现秒表计时器示例代码: ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Stopwatch implements ActionListener { private JFrame frame; private JLabel timeLabel; private JButton startButton, stopButton, resetButton; private Timer timer; private int elapsedTime; private int seconds; private int minutes; private int hours; private boolean started; public Stopwatch() { frame = new JFrame("Stopwatch"); frame.setSize(300, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); timeLabel = new JLabel("00:00:00", JLabel.CENTER); startButton = new JButton("Start"); stopButton = new JButton("Stop"); resetButton = new JButton("Reset"); startButton.addActionListener(this); stopButton.addActionListener(this); resetButton.addActionListener(this); timer = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent e) { elapsedTime += 1000; updateTimer(); } }); JPanel panel = new JPanel(new GridLayout(2, 1)); JPanel topPanel = new JPanel(new GridLayout(1, 1)); JPanel bottomPanel = new JPanel(new GridLayout(1, 3)); topPanel.add(timeLabel); bottomPanel.add(startButton); bottomPanel.add(stopButton); bottomPanel.add(resetButton); panel.add(topPanel); panel.add(bottomPanel); frame.add(panel); frame.setVisible(true); } public static void main(String[] args) { new Stopwatch(); } public void actionPerformed(ActionEvent e) { if (e.getSource() == startButton) { start(); } else if (e.getSource() == stopButton) { stop(); } else if (e.getSource() == resetButton) { reset(); } } private void start() { if (!started) { started = true; timer.start(); } } private void stop() { if (started) { started = false; timer.stop(); } } private void reset() { started = false; timer.stop(); elapsedTime = 0; seconds = 0; minutes = 0; hours = 0; updateTimer(); } private void updateTimer() { seconds = (elapsedTime / 1000) % 60; minutes = (elapsedTime / 60000) % 60; hours = (elapsedTime / 3600000) % 24; String secondsStr = String.format("%02d", seconds); String minutesStr = String.format("%02d", minutes); String hoursStr = String.format("%02d", hours); timeLabel.setText(hoursStr + ":" + minutesStr + ":" + secondsStr); } } ``` 这个示例代码实现了一个基本的秒表计时器功能,包括开始、停止和重置功能。它使用了Java Swing中的JFrame、JLabel、JButton和JPanel组件,以及Timer类来实现计时器的功能。当用户点击开始按钮时,计时器会开始计时;当用户点击停止按钮时,计时器会停止计时;当用户点击重置按钮时,计时器会重置为0。计时器的显示格式为:小时:分钟:秒钟。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值