AndroidStudio項(xiàng)目制作倒計(jì)時(shí)模塊的方法
前言
大家好,我是 Vic,今天給大家?guī)?lái)AndroidStudio項(xiàng)目制作倒計(jì)時(shí)模塊的概述,希望你們喜歡
項(xiàng)目難度
AndroidStudio項(xiàng)目制作倒計(jì)時(shí)模塊的難度,不是很大,就是主要用了Timer和TimerTask這兩個(gè),接著就是現(xiàn)實(shí)界面的一些基礎(chǔ)效果。
設(shè)計(jì)界面
做個(gè)倒計(jì)時(shí)的界面就比較好想了,就如下界面控件
- 填寫(xiě)倒計(jì)時(shí)時(shí)間
- 獲取倒計(jì)時(shí)時(shí)間
- 顯示倒計(jì)時(shí)
- 開(kāi)始計(jì)時(shí)
- 停止計(jì)時(shí)
就在自動(dòng)創(chuàng)建的activity_main.xml中寫(xiě)入代碼:
<?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"> <!--填寫(xiě)倒計(jì)時(shí)時(shí)間--> <EditText android:id="@+id/input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10"/> <!--獲取倒計(jì)時(shí)時(shí)間--> <Button android:id="@+id/get" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="獲取倒計(jì)時(shí)時(shí)間"/> <!--顯示倒計(jì)時(shí)--> <TextView android:id="@+id/time" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!--開(kāi)始計(jì)時(shí)--> <Button android:id="@+id/starttime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開(kāi)始計(jì)時(shí)"/> <!--停止計(jì)時(shí)--> <Button android:id="@+id/stoptime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止計(jì)時(shí)"/> </LinearLayout>
實(shí)現(xiàn)功能需求
接下來(lái)我們需要在MainActivity.java中現(xiàn)實(shí)功能模塊需求,主要來(lái)顯示界面和獲取按鈕功能效果,代碼如下:
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); getTime.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(); } }
心得重點(diǎn)
//獲取的按鈕實(shí)現(xiàn): 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(); }; }; //倒計(jì)時(shí)主要核心 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); }
總結(jié)
本文講了AndroidStudio項(xiàng)目制作倒計(jì)時(shí)模塊,如果您還有更好地理解,歡迎溝通
定位:分享 Android&Java知識(shí)點(diǎn),有興趣可以繼續(xù)關(guān)注,也希望大家多多支持腳本之家。
- android自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開(kāi)屏布局
- Android 實(shí)現(xiàn)閃屏頁(yè)和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計(jì)時(shí)效果
- Android賬號(hào)注冊(cè)實(shí)現(xiàn)點(diǎn)擊獲取驗(yàn)證碼倒計(jì)時(shí)效果
- Android自定義圓形倒計(jì)時(shí)進(jìn)度條
- Android開(kāi)發(fā)之獲取短信驗(yàn)證碼后按鈕背景變化并且出現(xiàn)倒計(jì)時(shí)
相關(guān)文章
Android Retrofit 中文亂碼問(wèn)題的解決辦法
這篇文章主要介紹了Android Retrofit 中文亂碼問(wèn)題的解決辦法的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家遇到這種問(wèn)題及時(shí)的解決,需要的朋友可以參考下2017-10-10Android使用WebView.loadUri()打開(kāi)網(wǎng)頁(yè)的方法
這篇文章主要介紹了Android使用WebView.loadUri()打開(kāi)網(wǎng)頁(yè)的方法,結(jié)合實(shí)例形式分析了Android中WebView控件的loadUri()打開(kāi)網(wǎng)頁(yè)的使用技巧,需要的朋友可以參考下2016-01-01Android通過(guò)Service實(shí)現(xiàn)簡(jiǎn)單的音樂(lè)播放
這篇文章主要介紹了Android通過(guò)Service實(shí)現(xiàn)簡(jiǎn)單的音樂(lè)播放,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Android實(shí)現(xiàn)新增及編輯聯(lián)系人的方法
這篇文章主要介紹了Android實(shí)現(xiàn)新增及編輯聯(lián)系人的方法,是Android應(yīng)用開(kāi)發(fā)常見(jiàn)的功能,需要的朋友可以參考下2014-07-07Android音頻系統(tǒng)AudioTrack使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android音頻系統(tǒng)AudioTrack的使用方法,如何使用AudioTrack進(jìn)行音頻播放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android 解決build path errors的問(wèn)題
這篇文章主要介紹了Android 解決build path errors的問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-09-09android ListView的右邊滾動(dòng)滑塊啟用方法 分享
android ListView的右邊滾動(dòng)滑塊啟用方法 分享,需要的朋友可以參考一下2013-05-05