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

  • 相关阅读:
    http协议相关知识
    linux 常用命令总结
    PHP traits
    php 正则案例
    php 中关于正则 元字符
    【U3D】 第三人称控制器ThirdPersonCharacter添加之后角色原地打转不移动的问题(unity5.3.5f)
    .Net Core异步async/await探索
    IdentityServer4实现单点登录统一认证
    CSAPP-Tiny Web服务器【2】源码解析
    CSAPP-Tiny Web服务器【1】编译搭建
  • 原文地址:https://www.cnblogs.com/kerwincui/p/14900356.html
Copyright © 2011-2022 走看看