怎么在AndroidStudio項(xiàng)目中制作一個(gè)倒計(jì)時(shí)模塊-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)公司成立與2013年,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元德清做網(wǎng)站,已為上家服務(wù),為德清各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話(huà):18982081108

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

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

網(wǎng)頁(yè)標(biāo)題:怎么在AndroidStudio項(xiàng)目中制作一個(gè)倒計(jì)時(shí)模塊-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://muchs.cn/article18/hssdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、企業(yè)建站、微信公眾號(hào)、網(wǎng)站內(nèi)鏈網(wǎng)站設(shè)計(jì)公司、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化