• Android学习之多线程复习——倒计时


    今天周六,离职在家,复习了一下多线程的一些知识

    然后写了一个简单的倒计时程序:

    以下是我的activity_main的布局文件:

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent" 
     5     android:orientation="vertical">
     6 
     7     <EditText 
     8         android:layout_width="fill_parent"
     9         android:layout_height="wrap_content"
    10         android:id="@+id/etTime"/>
    11     
    12     <Button
    13         android:id="@+id/btnGetTime"
    14         android:layout_width="wrap_content"
    15         android:layout_height="wrap_content"
    16         android:text="@string/countTime" />
    17 
    18     <TextView
    19         android:id="@+id/tvShowTime"
    20         android:layout_width="fill_parent"
    21         android:layout_height="wrap_content"
    22         android:text="" />
    23 
    24     <Button
    25         android:id="@+id/btnStart"
    26         android:layout_width="wrap_content"
    27         android:layout_height="wrap_content"
    28         android:text="@string/start" />
    29 
    30     <Button
    31         android:id="@+id/btnStop"
    32         android:layout_width="wrap_content"
    33         android:layout_height="wrap_content"
    34         android:text="@string/stop" />
    35 
    36 </LinearLayout>
    View Code

    就是一个EditText,一个TextView,三个按钮,没了
    然后就是我的MainActivity的代码:

     1 package com.oysd.counttime;
     2 
     3 import java.util.Timer;
     4 import java.util.TimerTask;
     5 
     6 import android.app.Activity;
     7 import android.os.Bundle;
     8 import android.os.Handler;
     9 import android.os.Message;
    10 import android.view.Menu;
    11 import android.view.MenuItem;
    12 import android.view.View;
    13 import android.view.View.OnClickListener;
    14 import android.widget.Button;
    15 import android.widget.EditText;
    16 import android.widget.TextView;
    17 
    18 public class MainActivity extends Activity implements OnClickListener {
    19     
    20     //声明一下要用到的控件
    21     private EditText etTime;
    22     private Button btnGetTime,btnStart,btnStop;
    23     private TextView tvShowTime;
    24     private int i = 0;
    25     private Timer timer;
    26     private TimerTask task;
    27 
    28     private Handler mHandler = new Handler(){
    29         public void handleMessage(android.os.Message msg) {
    30             tvShowTime.setText(msg.arg1 + "");
    31             startTime();
    32         };
    33     };
    34     
    35     @Override
    36     protected void onCreate(Bundle savedInstanceState) {
    37         super.onCreate(savedInstanceState);
    38         setContentView(R.layout.activity_main);
    39         initView();
    40     }
    41     
    42     private void initView(){
    43         etTime = (EditText) findViewById(R.id.etTime);
    44         tvShowTime = (TextView) findViewById(R.id.tvShowTime);
    45         btnGetTime = (Button) findViewById(R.id.btnGetTime);
    46         btnStart = (Button) findViewById(R.id.btnStart);
    47         btnStop = (Button) findViewById(R.id.btnStop);
    48         
    49         btnGetTime.setOnClickListener(this);
    50         btnStart.setOnClickListener(this);
    51         btnStop.setOnClickListener(this);
    52     }
    53 
    54     @Override
    55     public void onClick(View v) {
    56         switch(v.getId()){
    57         case R.id.btnGetTime:
    58             if(etTime.getText().toString().equals("")){
    59                 tvShowTime.setText(0+"");
    60                 i = 0;
    61             }else{
    62                 tvShowTime.setText(etTime.getText().toString());
    63                 i = Integer.parseInt(tvShowTime.getText().toString());
    64             }
    65             break;
    66         case R.id.btnStart:
    67             startTime();
    68             break;
    69         case R.id.btnStop:
    70             stopTime();
    71             break;
    72         }
    73     }
    74     
    75     private void startTime(){
    76         timer = new Timer();
    77         task = new TimerTask() {
    78             
    79             @Override
    80             public void run() {
    81                 i--;
    82                 Message message = new Message();
    83                 message.arg1 = i;
    84                 mHandler.sendMessage(message);
    85             }
    86         };
    87         timer.schedule(task, 1000);
    88     }
    89     
    90     private void stopTime(){
    91         timer.cancel();
    92     }
    93 }
    View Code

    在子线程里面,不要试图去修改主线程UI线程,不然会使界面响应卡死,需要用到handleMessage来操作UI线程

    另外,使用Timer的同时,必定也要使用TimerTask,来实现时间的倒计时

  • 相关阅读:
    java.io.IOException: HTTPS hostname wrong: should be 规格严格
    linux syslog 规格严格
    SVN,HG,GIT 命令说明 规格严格
    pclose : no child process 规格严格
    使用NetBeans6开发OSGi应用(1)——FirstOSGi[88250原创]
    Netbeans大战Eclipse 谁将走向祭坛?
    XP中的重要惯例和规则
    使用NetBeans6开发OSGi应用(1)——FirstOSGi[88250原创]
    简简单单删除所有.svn目录
    简简单单删除所有.svn目录
  • 原文地址:https://www.cnblogs.com/ouyangduoduo/p/4638912.html
Copyright © 2020-2023  润新知