zoukankan      html  css  js  c++  java
  • handler------post传送方式

     1 package com.qianfeng.gp08_day26_hanlder2;
     2 
     3 import android.os.Bundle;
     4 import android.os.Handler;
     5 import android.app.Activity;
     6 import android.view.Menu;
     7 import android.view.View;
     8 import android.widget.Button;
     9 import android.widget.TextView;
    10 
    11 public class MainActivity extends Activity {
    12 
    13     private TextView textView;
    14     private Button btn1,btn2;
    15     private Handler handler = new Handler();
    16     private int count;
    17     private boolean isStop;
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         super.onCreate(savedInstanceState);
    21         setContentView(R.layout.activity_main);
    22         
    23         textView = (TextView) findViewById(R.id.textView);
    24         btn1 = (Button) findViewById(R.id.btn1);
    25         btn2 = (Button) findViewById(R.id.btn2);
    26         
    27         btn2.setEnabled(false);
    28         
    29     }
    30 
    31     public void startTime(View v)//开始计时
    32     {
    33         isStop = false;
    34         btn1.setEnabled(false);
    35         btn2.setEnabled(true);
    36         new Thread(new Runnable(){
    37            
    38             @Override
    39             public void run() {
    40                 // TODO 向主线程发送计数
    41                  count=0;
    42                 while(!isStop)
    43                 {  
    44                     handler.post(new Runnable(){
    45                         @Override
    46                         public void run() {
    47                             textView.setText("计数器:"+count++);
    48                         }
    49                     });
    50                     try {
    51                         Thread.sleep(1000);
    52                     } catch (InterruptedException e) {
    53                         e.printStackTrace();
    54                     }
    55                 }
    56             }
    57             
    58         }).start();
    59     }
    60     
    61     public void stopTime(View v)
    62     {
    63         btn1.setEnabled(true);
    64         btn2.setEnabled(false);
    65         isStop = true;
    66     }
    67 }
    mainActivity
  • 相关阅读:
    C++PRIMER 阅读笔记 第三章
    一个for循环打印二维数组
    递归实现数组求和
    strlen 与 sizeof
    call,apply,bind,this
    js 原型继承
    vue 动画
    vuex学习心得
    vue+elementui dropdown 下拉菜单绑定方法
    vue 生命周期一点学习
  • 原文地址:https://www.cnblogs.com/bimingcong/p/4822424.html
Copyright © 2011-2022 走看看