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

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

时间:2015-10-08 23:18:23      阅读:577      评论:0      收藏:0      [点我收藏+]

标签: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了,欢迎大家贡献代码啊——传送门

版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp

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

标签:android   开发   秒表   计时器   时钟   

原文地址:http://blog.csdn.net/nomasp/article/details/48980007

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