前言
大家好,給大家帶來AndroidStudio項目制作倒計時模塊
的概述,希望你們喜歡
項目難度
AndroidStudio項目制作倒計時模塊的難度,不是很大,就是主要用了Timer
和TimerTask
這兩個,接着就是現實界面的一些基礎效果。
設計界面
做個倒計時的界面就比較好想了,就如下界面控件
- 填寫倒計時時間
- 獲取倒計時時間
- 顯示倒計時
- 開始計時
- 停止計時
就在自動創建的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
知識點,有興趣可以繼續關注