Preface
Hello everyone, I am Vic. Today I will give you an overview of the countdown module for Android Studio project production. I hope you like it
Project difficulty
The difficulty of making a countdown module for Android Studio project is either very high, but it mainly uses Timer and TimerTask, followed by some basic effects of the real interface.
Design interface
It's easier to think about making a countdown interface, just the following interface controls
- Fill in the countdown time
- Get the countdown time
- Show countdown
- Start timing
- Stop timing
Just write the code in the automatically created activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=""> <!--Fill in the countdown time--> <EditText android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10"/> <!--Get the countdown time--> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get countdown time"/> <!--Show countdown--> <TextView android: android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!--Start timing--> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start timing"/> <!--Stop timing--> <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop timing"/> </LinearLayout>
Implement functional requirements
Next, we need to realize the functional module requirements in the actual content, mainly to display the interface and obtain the function effects of the buttons, the code is as follows:
package ; import ; import ; import .; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends AppCompatActivity implements { 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) { (savedInstanceState); setContentView(.activity_main); initView(); } private void initView() { inputet = findViewById(); get = findViewById(); startTime = findViewById(); stopTime = findViewById(); time = findViewById(); (this); (this); (this); } @Override public void onClick(View v) { switch (()) { case : (().toString()); i = (().toString()); break; case : startTime(); break; case : stopTime(); break; default: break; } } private Handler mHandler = new Handler() { public void handleMessage(Message msg) { (msg.arg1 + ""); startTime(); }; }; public void startTime() { timer = new Timer(); task = new TimerTask() { @Override public void run() { if (i > 0) { //The joining judgment cannot be less than 0 i--; Message message = (); message.arg1 = i; (message); } } }; (task, 1000); } public void stopTime(){ (); } }
Key points of experience
//The button obtained is implemented:(().toString()); i = (().toString()); //Handler's joiningprivate Handler mHandler = new Handler() { public void handleMessage(Message msg) { (msg.arg1 + ""); startTime(); }; }; //The main core countdownpublic void startTime() { timer = new Timer(); task = new TimerTask() { @Override public void run() { if (i > 0) { //The joining judgment cannot be less than 0 i--; Message message = (); message.arg1 = i; (message); } } }; (task, 1000); }
Summarize
This article talks about the countdown module for Android Studio project production. If you have a better understanding, please feel free to communicate.
Positioning: Share Android & Java knowledge points. If you are interested, you can continue to pay attention. I also hope everyone will support me more.