最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

AndroidStudio項(xiàng)目制作倒計(jì)時(shí)模塊的方法

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

前言

大家好,我是 Vic,今天給大家?guī)?lái)AndroidStudio項(xiàng)目制作倒計(jì)時(shí)模塊的概述,希望你們喜歡

項(xiàng)目難度

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

設(shè)計(jì)界面

做個(gè)倒計(jì)時(shí)的界面就比較好想了,就如下界面控件

  1. 填寫(xiě)倒計(jì)時(shí)時(shí)間
  2. 獲取倒計(jì)時(shí)時(shí)間
  3. 顯示倒計(jì)時(shí)
  4. 開(kāi)始計(jì)時(shí)
  5. 停止計(jì)時(shí)

就在自動(dòng)創(chuàng)建的activity_main.xml中寫(xiě)入代碼:

<?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">
  <!--填寫(xiě)倒計(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"
    />
  <!--開(kāi)始計(jì)時(shí)-->
  <Button
    android:id="@+id/starttime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="開(kāi)始計(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)功能需求

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

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

總結(jié)

本文講了AndroidStudio項(xiàng)目制作倒計(jì)時(shí)模塊,如果您還有更好地理解,歡迎溝通

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

相關(guān)文章

  • Android?實(shí)現(xiàn)APP可切換多語(yǔ)言步驟詳解

    Android?實(shí)現(xiàn)APP可切換多語(yǔ)言步驟詳解

    如果是單獨(dú)給app加上國(guó)際化,其實(shí)很容易,創(chuàng)建對(duì)應(yīng)的國(guó)家資源文件夾即可,如values-en,values-pt,這篇文章主要介紹了Android?實(shí)現(xiàn)APP可切換多語(yǔ)言,需要的朋友可以參考下
    2023-11-11
  • Android開(kāi)發(fā)壁紙的驗(yàn)證設(shè)置和確認(rèn)功能實(shí)現(xiàn)demo

    Android開(kāi)發(fā)壁紙的驗(yàn)證設(shè)置和確認(rèn)功能實(shí)現(xiàn)demo

    android?wallpaper包括鎖屏壁紙和桌面壁紙,壁紙又區(qū)分靜態(tài)和動(dòng)態(tài)兩種。本文詳細(xì)介紹靜態(tài)壁紙?jiān)O(shè)置和確認(rèn),有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-04-04
  • Android開(kāi)發(fā)之Notification通知用法詳解

    Android開(kāi)發(fā)之Notification通知用法詳解

    這篇文章主要介紹了Android開(kāi)發(fā)之Notification通知用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Notification通知的功能、參數(shù)、定義及使用方法,需要的朋友可以參考下
    2016-11-11
  • Android獲取文字高度的三種方法

    Android獲取文字高度的三種方法

    這篇文章主要給大家介紹了Android獲取文字高度的三種方法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Android RecyclerView實(shí)現(xiàn)拼團(tuán)倒計(jì)時(shí)列表實(shí)例代碼

    Android RecyclerView實(shí)現(xiàn)拼團(tuán)倒計(jì)時(shí)列表實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于Android RecyclerView實(shí)現(xiàn)拼團(tuán)倒計(jì)時(shí)列表的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析

    Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析

    StrictMode意思為嚴(yán)格模式,是用來(lái)檢測(cè)程序中違例情況的開(kāi)發(fā)者工具。最常用的場(chǎng)景就是檢測(cè)主線程中本地磁盤(pán)和網(wǎng)絡(luò)讀寫(xiě)等耗時(shí)的操作。這篇文章給大家介紹Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析,感興趣的朋友一起看看吧
    2018-01-01
  • 將cantk runtime嵌入到現(xiàn)有的APP中的方法

    將cantk runtime嵌入到現(xiàn)有的APP中的方法

    今天小編就為大家分享一篇關(guān)于將cantk runtime嵌入到現(xiàn)有的APP中的方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • Android中一種巧妙的drawable.xml替代方案分享

    Android中一種巧妙的drawable.xml替代方案分享

    這篇文章主要給大家介紹了關(guān)于Android中一種巧妙的drawable.xml替代方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Android測(cè)試方法總結(jié)

    Android測(cè)試方法總結(jié)

    在這篇文章中我們給大家總結(jié)了Android測(cè)試方法以及需要注意的地方,有興趣的朋友參考學(xué)習(xí)下。
    2018-07-07
  • Android開(kāi)發(fā)實(shí)現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法

    Android開(kāi)發(fā)實(shí)現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法

    這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)Launcher3應(yīng)用列表修改透明背景的方法,結(jié)合實(shí)例形式分析了Launcher3相關(guān)配置文件與功能函數(shù)修改設(shè)置操作技巧,需要的朋友可以參考下
    2017-11-11

最新評(píng)論

马山县| 巴林左旗| 太仆寺旗| 庆元县| 临江市| 宣化县| 静宁县| 富阳市| 英超| 咸宁市| 大理市| 安平县| 仁怀市| 梅州市| 赤水市| 淮南市| 固安县| 惠水县| 湟源县| 义马市| 兴宁市| 英超| 齐齐哈尔市| 梓潼县| 武功县| 平原县| 白沙| 青河县| 措美县| 冀州市| 诸城市| 都安| 峨眉山市| 中方县| 芦山县| 英超| 吐鲁番市| 固镇县| 达拉特旗| 义马市| 兰考县|