亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

AndroidStudio項目制作倒計時模塊的方法

 更新時間:2018年04月04日 10:02:39   作者:Vic_NeTech  
本篇文章主要介紹了AndroidStudio項目制作倒計時模塊的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

前言

大家好,我是 Vic,今天給大家?guī)鞟ndroidStudio項目制作倒計時模塊的概述,希望你們喜歡

項目難度

AndroidStudio項目制作倒計時模塊的難度,不是很大,就是主要用了Timer和TimerTask這兩個,接著就是現(xiàn)實界面的一些基礎(chǔ)效果。

設(shè)計界面

做個倒計時的界面就比較好想了,就如下界面控件

  1. 填寫倒計時時間
  2. 獲取倒計時時間
  3. 顯示倒計時
  4. 開始計時
  5. 停止計時

就在自動創(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">
  <!--填寫倒計時時間-->
  <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>

實現(xiàn)功能需求

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

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

心得重點

//獲取的按鈕實現(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();
    };
  };
//倒計時主要核心
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);
  }

總結(jié)

本文講了AndroidStudio項目制作倒計時模塊,如果您還有更好地理解,歡迎溝通

定位:分享 Android&Java知識點,有興趣可以繼續(xù)關(guān)注,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論