AndroidStudio項目制作倒計時模塊的方法
前言
大家好,我是 Vic,今天給大家?guī)鞟ndroidStudio項目制作倒計時模塊的概述,希望你們喜歡
項目難度
AndroidStudio項目制作倒計時模塊的難度,不是很大,就是主要用了Timer和TimerTask這兩個,接著就是現(xiàn)實界面的一些基礎(chǔ)效果。
設(shè)計界面
做個倒計時的界面就比較好想了,就如下界面控件
- 填寫倒計時時間
- 獲取倒計時時間
- 顯示倒計時
- 開始計時
- 停止計時
就在自動創(chuàng)建的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>
實現(xiàn)功能需求
接下來我們需要在MainActivity.java中現(xiàn)實功能模塊需求,主要來顯示界面和獲取按鈕功能效果,代碼如下:
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();
}
}
心得重點
//獲取的按鈕實現(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();
};
};
//倒計時主要核心
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項目制作倒計時模塊,如果您還有更好地理解,歡迎溝通
定位:分享 Android&Java知識點,有興趣可以繼續(xù)關(guān)注,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)壁紙的驗證設(shè)置和確認功能實現(xiàn)demo
android?wallpaper包括鎖屏壁紙和桌面壁紙,壁紙又區(qū)分靜態(tài)和動態(tài)兩種。本文詳細介紹靜態(tài)壁紙設(shè)置和確認,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-04-04
Android開發(fā)之Notification通知用法詳解
這篇文章主要介紹了Android開發(fā)之Notification通知用法,結(jié)合實例形式較為詳細的分析了Notification通知的功能、參數(shù)、定義及使用方法,需要的朋友可以參考下2016-11-11
Android RecyclerView實現(xiàn)拼團倒計時列表實例代碼
這篇文章主要給大家介紹了關(guān)于Android RecyclerView實現(xiàn)拼團倒計時列表的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-09-09
Android性能調(diào)優(yōu)利器StrictMode應用分析
StrictMode意思為嚴格模式,是用來檢測程序中違例情況的開發(fā)者工具。最常用的場景就是檢測主線程中本地磁盤和網(wǎng)絡讀寫等耗時的操作。這篇文章給大家介紹Android性能調(diào)優(yōu)利器StrictMode應用分析,感興趣的朋友一起看看吧2018-01-01
將cantk runtime嵌入到現(xiàn)有的APP中的方法
今天小編就為大家分享一篇關(guān)于將cantk runtime嵌入到現(xiàn)有的APP中的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
Android中一種巧妙的drawable.xml替代方案分享
這篇文章主要給大家介紹了關(guān)于Android中一種巧妙的drawable.xml替代方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-09-09
Android開發(fā)實現(xiàn)Launcher3應用列表修改透明背景的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)Launcher3應用列表修改透明背景的方法,結(jié)合實例形式分析了Launcher3相關(guān)配置文件與功能函數(shù)修改設(shè)置操作技巧,需要的朋友可以參考下2017-11-11

