android基礎:handler與messag案例(計時器)

來源:互聯網
上載者:User

標籤:

 

運行效果

 

代碼檔案

string.xml

<?xml version="1.0" encoding="utf-8"?>  <resources>      <string name="action_settings">Settings</string>      <string name="app_name">計時器</string>      <string name="gettime">計時</string>      <string name="starttime">開始</string>      <string name="stoptime">停止</string>      <string name="inputhint">請輸入時間</string>  </resources>  

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:orientation="vertical"      tools:context="com.mytest.mycounttime.MainActivity" >        <EditText          android:id="@+id/inputTime"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:ems="10"          android:hint="@string/inputhint"          android:inputType="number" >          <requestFocus />      </EditText>        <Button          android:id="@+id/getTime"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/gettime" />        <Button          android:id="@+id/startTime"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/starttime" />        <Button          android:id="@+id/stopTime"          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="@string/stoptime" />        <TextView          android:id="@+id/timeView"          android:layout_width="wrap_content"          android:layout_height="wrap_content" />    </LinearLayout> 

 

activity.java

package com.mytest.mycounttime;    import java.util.Timer;  import java.util.TimerTask;    import android.app.Activity;  import android.os.Bundle;  import android.os.Handler;  import android.os.Message;  import android.util.Log;  import android.view.Menu;  import android.view.MenuItem;  import android.view.View;  import android.widget.Button;  import android.widget.EditText;  import android.widget.TextView;    public class MainActivity extends Activity {        private Button getTimeButton, startTimeButton, stopTimeButton;      private EditText inputTimeText;      private TextView timeViewText;        private int inputTime;      private int currTime;      private Timer timer;      private TimerTask timerTask;      private Handler handler = new MessageHandler();        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            // 頁面元素賦值          initView();            getTimeButton.setOnClickListener(new TimeButtonOnClickListener());          startTimeButton.setOnClickListener(new TimeButtonOnClickListener());          stopTimeButton.setOnClickListener(new TimeButtonOnClickListener());        }        private void initView() {          getTimeButton = (Button) this.findViewById(R.id.getTime);          startTimeButton = (Button) this.findViewById(R.id.startTime);          stopTimeButton = (Button) this.findViewById(R.id.stopTime);          inputTimeText = (EditText) this.findViewById(R.id.inputTime);          timeViewText = (TextView) this.findViewById(R.id.timeView);      }        private final class MessageHandler extends Handler {            @Override          public void handleMessage(Message msg) {              // timeViewText.setText(currTime);              // 顯示目前時間              timeViewText.setText("時間:"+msg.arg1 + "");              startTime();          }        }        private final class TimeButtonOnClickListener implements View.OnClickListener {            @Override          public void onClick(View v) {              Log.v("info", "currTime:" + currTime);                switch (v.getId()) {              case R.id.getTime:                  inputTime = Integer.parseInt(inputTimeText.getText().toString());                  currTime = inputTime;                  break;                case R.id.startTime:                  startTime();                  break;              case R.id.stopTime:                  stopTime();                  break;                }            }        }        private void stopTime() {          timer.cancel();        }        private void startTime() {            timer = new Timer();          timerTask = new TimerTask() {                @Override              public void run() {                    Log.v("info", "currTime:" + currTime);                    if (currTime <= 0) {                      timer.cancel();                      return;                  }                    currTime--;                    // 發送訊息,傳送目前時間                  Message message = handler.obtainMessage();                  message.arg1 = currTime;                  handler.sendMessage(message);              }            };            timer.schedule(timerTask, 1000);        }    }  

  

android基礎:handler與messag案例(計時器)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.