溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在AndroidStudio項(xiàng)目中制作一個(gè)倒計(jì)時(shí)模塊

發(fā)布時(shí)間:2021-03-11 16:06:38 來源:億速云 閱讀:243 作者:Leah 欄目:移動(dòng)開發(fā)

今天就跟大家聊聊有關(guān)怎么在AndroidStudio項(xiàng)目中制作一個(gè)倒計(jì)時(shí)模塊,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)建的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">
  <!--填寫倒計(jì)時(shí)時(shí)間-->
  <EditText
    android:id="@+id/input"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"/>
  <!--獲取倒計(jì)時(shí)時(shí)間-->
  <Button
    android:id="@+id/get"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="獲取倒計(jì)時(shí)時(shí)間"/>
  <!--顯示倒計(jì)時(shí)-->
  <TextView
    android:id="@+id/time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
  <!--開始計(jì)時(shí)-->
  <Button
    android:id="@+id/starttime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="開始計(jì)時(shí)"/>
  <!--停止計(jì)時(shí)-->
  <Button
    android:id="@+id/stoptime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="停止計(jì)時(shí)"/>
</LinearLayout>

實(shí)現(xiàn)功能需求

接下來我們需要在MainActivity.java中現(xiàn)實(shí)功能模塊需求,主要來顯示界面和獲取按鈕功能效果,代碼如下:

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();
  }
}

心得重點(diǎn)

//獲取的按鈕實(shí)現(xiàn):
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();
    };
  };
//倒計(jì)時(shí)主要核心
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);
  }

看完上述內(nèi)容,你們對怎么在AndroidStudio項(xiàng)目中制作一個(gè)倒計(jì)時(shí)模塊有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI