Android 开发第七弹:简易时钟(秒表)

简介:

本文承接,Android 开发第五弹:简易时钟(闹钟) 和 Android 开发第六弹:简易时钟(计时器),这一部分是关于秒表的。

这里写图片描述

布局

同样是新建一个类(StopWatchView)并扩展自LinearLayout,并将其用作布局。

<myapplication.nomasp.com.clock.StopWatchView
    android : id = "@+id/tabStopWatch"
    android : layout_width = "match_parent"
    android : layout_height = "match_parent"
    android : orientation = "vertical">

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

    <TextView
    android : id = "@+id/tvHour"
    android : layout_width = "0dp"
    android : layout_height = "wrap_content"
    android : layout_weight = "1"
    android : textAppearance = "?android:attr/textAppearanceLarge" / >

    <TextView
    android : text = ":"
    android : layout_width = "wrap_content"
    android : layout_height = "wrap_content"
    android : textAppearance = "?android:attr/textAppearanceLarge" / >

    <TextView
    android : id = "@+id/tvMinute"
    android : layout_width = "0dp"
    android : layout_height = "wrap_content"
    android : layout_weight = "1"
    android : textAppearance = "?android:attr/textAppearanceLarge" / >

    <TextView
    android : text = ":"
    android : layout_width = "wrap_content"
    android : layout_height = "wrap_content"
    android : textAppearance = "?android:attr/textAppearanceLarge" / >

    <TextView
    android : id = "@+id/tvSecond"
    android : layout_width = "0dp"
    android : layout_height = "wrap_content"
    android : layout_weight = "1"
    android : textAppearance = "?android:attr/textAppearanceLarge" / >

    <TextView
    android : text = "."
    android : layout_width = "wrap_content"
    android : layout_height = "wrap_content"
    android : textAppearance = "?android:attr/textAppearanceLarge" / >

    <TextView
    android : id = "@+id/tvMSceond"
    android : layout_width = "0dp"
    android : layout_height = "wrap_content"
    android : layout_weight = "1"
    android : textAppearance = "?android:attr/textAppearanceLarge" / >

    < / LinearLayout>

    <ListView
    android : id = "@+id/lvWatchTimeList"
    android : layout_width = "match_parent"
    android : layout_height = "0dp"
    android : layout_weight = "1">

    < / ListView>

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

    <Button
    android : id = "@+id/btnSWStart"
    android : layout_width = "0dp"
    android : layout_height = "wrap_content"
    android : layout_weight = "1"
    android : text = "@string/start" / >

    <Button
    android : id = "@+id/btnSWPause"
    android : layout_width = "0dp"
    android : layout_height = "wrap_content"
    android : layout_weight = "1"
    android : text = "@string/pause" / >

    <Button
    android : id = "@+id/btnSWResume"
    android : layout_width = "0dp"
    android : layout_height = "wrap_content"
    android : layout_weight = "1"
    android : text = "@string/resume" / >

    <Button
    android : id = "@+id/btnSWRecord"
    android : layout_width = "0dp"
    android : layout_height = "wrap_content"
    android : layout_weight = "1"
    android : text = "@string/record" / >

    <Button
    android : id = "@+id/btnSWReset"
    android : layout_width = "0dp"
    android : layout_height = "wrap_content"
    android : layout_weight = "1"
    android : text = "@string/reset" / >

    < / LinearLayout>
</myapplication.nomasp.com.clock.StopWatchView>

StopWatchView

同样是一开始要定义好的这些balabala的东西:

    private int tenMSecs = 0;
    private Timer timer = new Timer();
    private TimerTask timerTask = null;
    private TimerTask showTimeTask = null;

    private TextView tvHour, tvMinute, tvSecond, tvMSecond;
    private Button btnSWStart, btnSWResume, btnSWReset, btnSWPause, btnSWRecord;
    private ListView lvWatchTimeList;
    private ArrayAdapter<String> adapter;

    private static final int MSG_WHAT_SHOW_TIME = 1;

    public StopWatchView(Context context) {
        super(context);
    }

    public StopWatchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StopWatchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

看看这些注释,发现和前面两篇的也没有区别啊,我就不废话直接上代码好了。

 @Override
    protected void onFinishInflate(){
        super.onFinishInflate();

        // 为每个相应的TextView控件设置成0
        tvHour = (TextView)findViewById(R.id.tvHour);
        tvHour.setText("0");
        tvMinute = (TextView)findViewById(R.id.tvMinute);
        tvMinute.setText("0");
        tvSecond = (TextView)findViewById(R.id.tvSecond);
        tvSecond.setText("0");
        tvMSecond = (TextView)findViewById(R.id.tvMSceond);
        tvMSecond.setText("0");

        // 为每个Button设置监听事件
        btnSWRecord = (Button)findViewById(R.id.btnSWRecord);
        btnSWRecord.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.insert(String.format("%d:%d:%d.%d",
                        tenMSecs/100/60/60,
                        tenMSecs/100/60%60,
                        tenMSecs/100%60,
                        tenMSecs%100),
                        0);
            }
        });

        btnSWPause = (Button)findViewById(R.id.btnSWPause);
        btnSWPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 停止
                stopTimer();

                btnSWPause.setVisibility(View.GONE);
                btnSWResume.setVisibility(View.VISIBLE);
                btnSWReset.setVisibility(View.VISIBLE);
                btnSWRecord.setVisibility(View.GONE);
            }
        });

        btnSWReset = (Button)findViewById(R.id.btnSWReset);
        btnSWReset.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 停止
                stopTimer();
                tenMSecs = 0;
                adapter.clear();

                btnSWStart.setVisibility(View.VISIBLE);
                btnSWPause.setVisibility(View.GONE);
                btnSWReset.setVisibility(View.GONE);
                btnSWRecord.setVisibility(View.GONE);
                btnSWResume.setVisibility(View.GONE);
            }
        });

        btnSWResume = (Button)findViewById(R.id.btnSWResume);
        btnSWResume.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 开始
                startTimer();

                btnSWResume.setVisibility(View.GONE);
                btnSWReset.setVisibility(View.GONE);
                btnSWRecord.setVisibility(View.VISIBLE);
                btnSWPause.setVisibility(View.VISIBLE);
            }
        });

        btnSWStart = (Button)findViewById(R.id.btnSWStart);
        btnSWStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 开始
                startTimer();

                btnSWStart.setVisibility(View.GONE);
                btnSWPause.setVisibility(View.VISIBLE);
                btnSWRecord.setVisibility(View.VISIBLE);
            }
        });

        btnSWRecord.setVisibility(View.GONE);
        btnSWPause.setVisibility(View.GONE);
        btnSWReset.setVisibility(View.GONE);
        btnSWResume.setVisibility(View.GONE);

        // 将适配器添加到列表
        lvWatchTimeList = (ListView)findViewById(R.id.lvWatchTimeList);
        adapter = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_list_item_1);
        lvWatchTimeList.setAdapter(adapter);

        // 向Handler发送消息
        showTimeTask = new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(MSG_WHAT_SHOW_TIME);
            }
        };
        // 开始计时
        timer.schedule(showTimeTask,200,200);
    }

    // 开始
    private void startTimer(){
        if(timerTask == null){
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    tenMSecs++;
                }
            };
            timer.schedule(timerTask,10,10);
        }
    }

    // 结束
    private void stopTimer(){
        if(timerTask != null){
            timerTask.cancel();
            timerTask = null;
        }
    }

    // 取消计时
    public void onDestory(){
        timer.cancel();
    }

    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                // 如果消息匹配,则将相应时间计算后显示在相应TextView上
                case MSG_WHAT_SHOW_TIME:
                    tvHour.setText(tenMSecs/100/60/60+"");
                    tvMinute.setText(tenMSecs/100/60%60+"");
                    tvSecond.setText(tenMSecs/100%60+"");
                    tvMSecond.setText(tenMSecs%100+"");
                    break;
                default:
                    break;
            }
        };
    };

结束

好吧,这次是真的结束了。

同样的,需要代码就直接评论留邮箱吧。代码会继续更新的,注释也会继续更新……

项目也上传到Github了,欢迎大家贡献代码啊——传送门 。

目录
相关文章
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
2265 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
移动开发 JavaScript 应用服务中间件
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1440 5
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
移动开发 Rust JavaScript
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
1490 4
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
1301 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
移动开发 Android开发
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
777 0
|
Java 开发工具 Maven
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
1770 6
|
JavaScript Linux 网络安全
Termux安卓终端美化与开发实战:从下载到插件优化,小白也能玩转Linux
Termux是一款安卓平台上的开源终端模拟器,支持apt包管理、SSH连接及Python/Node.js/C++开发环境搭建,被誉为“手机上的Linux系统”。其特点包括零ROOT权限、跨平台开发和强大扩展性。本文详细介绍其安装准备、基础与高级环境配置、必备插件推荐、常见问题解决方法以及延伸学习资源,帮助用户充分利用Termux进行开发与学习。适用于Android 7+设备,原创内容转载请注明来源。
7677 77
|
安全 数据库 Android开发
在Android开发中实现两个Intent跳转及数据交换的方法
总结上述内容,在Android开发中,Intent不仅是活动跳转的桥梁,也是两个活动之间进行数据交换的媒介。运用Intent传递数据时需注意数据类型、传输大小限制以及安全性问题的处理,以确保应用的健壯性和安全性。
884 11
|
安全 Java Android开发
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
681 0
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
|
移动开发 Java 编译器
Kotlin与Jetpack Compose:Android开发生态的演进与架构思考
本文从资深Android工程师视角深入分析Kotlin与Jetpack Compose在Android系统中的技术定位。Kotlin通过空安全、协程等特性解决了Java在移动开发中的痛点,成为Android官方首选语言。Jetpack Compose则引入声明式UI范式,通过重组机制实现高效UI更新。两者结合不仅提升开发效率,更为跨平台战略和现代架构模式提供技术基础,代表了Android开发生态的根本性演进。
735 0

热门文章

最新文章