zoukankan      html  css  js  c++  java
  • android实现计时器(转)

     

    新建布局文件activity_main.xml

     
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/btnOk"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开始计时"
            android:textSize="15dp" />
    
        <TextView
            android:id="@+id/tvTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15dp" />
    
    
    </LinearLayout>
     

    新建 MainActivity.java

     
    package com.example.administrator.timer;
    
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Button btnOk;
        private TextView tvTime;
    
        private boolean bStart=false;
        private Handler mHander=new Handler();
        private int mCount=0;
    
        private Runnable mCounter=new Runnable() {
            @Override
            public void run() {
                mCount++;
                tvTime.setText("当前计数值:"+mCount);
                mHander.postDelayed(this,1000);
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initView();
        }
    
        private void initView() {
            btnOk = (Button) findViewById(R.id.btnOk);
            tvTime = (TextView) findViewById(R.id.tvTime);
    
            btnOk.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btnOk:
                    if (bStart==false){
                        btnOk.setText("停止计时");
                        mHander.post(mCounter);
                    }else{
                        btnOk.setText("开始计时");
                        mHander.removeCallbacks(mCounter);
                    }
                    bStart=!bStart;
                    break;
            }
        }
    }
     

    发中经常利用Handler启动任务。下面是Handler处理Runnable任务的常见方法说明:

    ●post: 立即启动Runnable任务。

    ●postDelayed:延迟若干时间后启动Runnable任务。

    ●postAtTime:在指定时间启动Runnable任务。

    ●removeCallbacks: 移除指定的Runnable任务。

    原文:https://www.cnblogs.com/ldy731729142/p/12608820.html

  • 相关阅读:
    c++中重载、重写、覆盖的区别
    c++值传递,指针传递,引用传递以及指针与引用的区别
    c/c++中static与extern关键字介绍
    Typedef和#define之间的区别
    c++中sizeof()的用法介绍
    c++笔试题:不使用第三个变量来交换俩个变量的数值
    c++位运算符介绍
    3.高并发教程-基础篇-之分布式全文搜索引擎elasticsearch的搭建
    2.高并发教程-基础篇-之nginx+mysql实现负载均衡和读写分离
    1.高并发教程-基础篇-之nginx负载均衡的搭建
  • 原文地址:https://www.cnblogs.com/kerwincui/p/14900356.html
Copyright © 2011-2022 走看看