Objective
Hello everyone, to bring you AndroidStudio项目制作倒计时模块 an overview, I hope you like
Project Difficulty
Androidstudio Project Production Countdown module difficulty, is not very big, is mainly used with Timer TimerTask these two, and then is the reality of some basic effects of the interface.
Design interface
A countdown to the interface is better to think about, the following interface controls
- Fill in the countdown time
- Get countdown time
- Show Countdown
- Start timing
- Stop timing
Write the code in the automatically created 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" > &L t;! --Fill in the countdown time-<edittext android:id= "@+id/input" android:layout_width= "Wrap_content" android:l ayout_height= "Wrap_content" android:ems= "ten"/> <!--Get countdown time-<button android:id= "@+id/get "Android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" Get Countdown Time "/> ; <!--display Countdown--<textview android:id= "@+id/time" android:layout_width= "Wrap_content" Android : layout_height= "Wrap_content"/> <!--start timing-<button android:id= "@+id/starttime" an Droid:layout_width= "Wrap_content "android:layout_height=" wrap_content "android:text=" Start timing/> <!--stop timing--<button Android:id= "@+id/stoptime" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "Stop Timing"/></linearlayout>
Implement functional requirements
Next we need to mainactivity.java in the actual function module requirements, mainly to display the interface and get button function effect, the code is as follows:
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 EditT Ext 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 ()) {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.sette XT (Msg.arg1 + ""); StartTime (); }; }; public void StartTime () {timer = new timer (); task = new TimerTask () {@Override public void run () {if (i > 0) {//join judgment cannot be less than 0 i--; Message message = Mhandler.obtainmessage (); MESSAGE.ARG1 = i; Mhandler.sendmessage (message); } } }; Timer.schedule (task, 1000); } public void StopTime () {timer.cancel (); }}
Key points of experience
//获取的按钮实现: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); }
Summarize
- This article is about the Androidstudio Project production Countdown module, if you have a better understanding, welcome to communicate
- Positioning: Sharing
Android & Java knowledge points, interested to continue to follow
Androidstudio Project Production Countdown module