欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

AndroidStudio项目制作倒计时模块的方法

时间:2021-03-28 09:18:12|栏目:Android代码|点击:

前言

大家好,我是 Vic,今天给大家带来AndroidStudio项目制作倒计时模块的概述,希望你们喜欢

项目难度

AndroidStudio项目制作倒计时模块的难度,不是很大,就是主要用了Timer和TimerTask这两个,接着就是现实界面的一些基础效果。

设计界面

做个倒计时的界面就比较好想了,就如下界面控件

  1. 填写倒计时时间
  2. 获取倒计时时间
  3. 显示倒计时
  4. 开始计时
  5. 停止计时

就在自动创建的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实现通话自动录音服务

栏    目:Android代码

下一篇:Android ListView 实现上拉加载的示例代码

本文标题:AndroidStudio项目制作倒计时模块的方法

本文地址:http://www.codeinn.net/misctech/89927.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有