AndroidStudio項目製作倒計時模組

來源:互聯網
上載者:User

標籤:http   odi   support   protect   tac   match   xtend   task   out   

前言

大家好,給大家帶來AndroidStudio項目製作倒計時模組的概述,希望你們喜歡

項目難度

AndroidStudio項目製作倒計時模組的難度,不是很大,就是主要用了TimerTimerTask這兩個,接著就是現實介面的一些基礎效果。

設計介面

做個倒計時的介面就比較好想了,就如下介面控制項

  • 填寫倒計時時間
  • 擷取倒計時時間
  • 顯示倒計時
  • 開始計時
  • 停止計時
    就在自動建立的activity_main.xml中寫入代碼:
<?xml version="1.0" encoding="utf-8"?><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="cn.edu.gdmec.android.counttime.MainActivity">    <!--填寫倒計時時間-->    <EditText        android:id="@+id/input"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:ems="10"/>    <!--擷取倒計時時間-->    <Button        android:id="@+id/get"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="擷取倒計時時間"/>    <!--顯示倒計時-->    <TextView        android:id="@+id/time"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        />    <!--開始計時-->    <Button        android:id="@+id/starttime"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="開始計時"/>    <!--停止計時-->    <Button        android:id="@+id/stoptime"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="停止計時"/></LinearLayout>
實現功能需求

接下來我們需要在MainActivity.java中現實功能模組需求,主要來顯示介面和擷取按鈕功能效果,代碼如下:

package cn.edu.gdmec.android.counttime;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import java.util.Timer;import java.util.TimerTask;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private EditText inputet;    private Button get, startTime, stopTime;    private TextView time;    private int i = 0;    private Timer timer = null;    private TimerTask task = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        inputet = findViewById(R.id.input);        get = findViewById(R.id.get);        startTime = findViewById(R.id.starttime);        stopTime = findViewById(R.id.stoptime);        time = findViewById(R.id.time);        get.setOnClickListener(this);        startTime.setOnClickListener(this);        stopTime.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.get:                time.setText(inputet.getText().toString());                i = Integer.parseInt(inputet.getText().toString());                break;            case R.id.starttime:                startTime();                break;            case R.id.stoptime:                stopTime();                break;            default:                break;        }    }    private Handler mHandler = new Handler() {        public void handleMessage(Message msg) {            time.setText(msg.arg1 + "");            startTime();        };    };    public void startTime() {        timer = new Timer();        task = new TimerTask() {            @Override            public void run() {                if (i > 0) {   //加入判斷不能小於0                    i--;                    Message message = mHandler.obtainMessage();                    message.arg1 = i;                    mHandler.sendMessage(message);                }            }        };        timer.schedule(task, 1000);    }    public void stopTime(){        timer.cancel();    }}
心得重點
//擷取的按鈕實現:time.setText(inputet.getText().toString());i = Integer.parseInt(inputet.getText().toString());//Handler的加入private Handler mHandler = new Handler() {        public void handleMessage(Message msg) {            time.setText(msg.arg1 + "");            startTime();        };    };//倒計時主要核心public void startTime() {        timer = new Timer();        task = new TimerTask() {            @Override            public void run() {                if (i > 0) {   //加入判斷不能小於0                    i--;                    Message message = mHandler.obtainMessage();                    message.arg1 = i;                    mHandler.sendMessage(message);                }            }        };        timer.schedule(task, 1000);    }
總結
  • 本文講了AndroidStudio項目製作倒計時模組,如果您還有更好地理解,歡迎溝通
  • 定位:分享 Android&Java知識點,有興趣可以繼續關注

AndroidStudio項目製作倒計時模組

聯繫我們

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