项目难度
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); 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(); }}
心得重点
//获取的按钮实现: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知识点,有兴趣可以继续关注