Android學習之多線程複習——倒計時

來源:互聯網
上載者:User

標籤:

今天周六,離職在家,複習了一下多線程的一些知識

然後寫了一個簡單的倒計時程式:

以下是我的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     <Button13         android:id="@+id/btnGetTime"14         android:layout_width="wrap_content"15         android:layout_height="wrap_content"16         android:text="@string/countTime" />17 18     <TextView19         android:id="@+id/tvShowTime"20         android:layout_width="fill_parent"21         android:layout_height="wrap_content"22         android:text="" />23 24     <Button25         android:id="@+id/btnStart"26         android:layout_width="wrap_content"27         android:layout_height="wrap_content"28         android:text="@string/start" />29 30     <Button31         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     @Override36     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     @Override55     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             @Override80             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,來實現時間的倒計時

Android學習之多線程複習——倒計時

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.