作者:程序員小冰,GitHub主頁:https://github.com/QQ986945193
新浪微博:http://weibo.com/mcxiaobing
首先給大家看一下我們今天這個最終實現的效果圖:
界面效果有點醜,不過功能齊全。大家如果需要,可以集成到自己的項目中。
首先說明,其實這個很簡單的,利用了一個定時器而已。不過我這裏進行了
輸入的時間進行判斷,比如不是Int類型均不可以進行倒計時。還有防止
多次點擊開始計時,最後就是進行了回收對象。好了給大家看一下代碼吧。
xml佈局比較簡單,幾個button與textview,edittext實現的。
<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">
<EditText
android:id="@+id/et_timer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入計時時間" />
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="獲取計時時間" />
<TextView
android:id="@+id/tv_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="計時時間" />
<Button
android:id="@+id/btn_go"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始計時" />
<Button
android:id="@+id/btn_stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止計時" />
</LinearLayout>
下面是java類中的代碼:
package davidtimerdown.qq986945193.com.davidtimerdown;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* @author :程序員小冰
* @新浪微博 :http://weibo.com/mcxiaobing
* @GitHub: https://github.com/QQ986945193
* @CSDN博客: http://blog.csdn.net/qq_21376985
* @碼雲OsChina :http://git.oschina.net/MCXIAOBING
*/
public class MainActivity extends Activity implements OnClickListener {
protected static final int MSG_WHAT = 0;
//開始倒計時
private Button btn_go;
//獲取到輸入的時間
private Button btn_get;
//停止倒計時
private Button btn_stop;
//輸入的時間
private static EditText et_time;
//時間
private static TextView tv_time;
private Timer timer;
private static int time;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
tv_time.setText(time + "");
switch (msg.what) {
case MSG_WHAT:
if (time > 0) {
time--;
} else {
Toast.makeText(MainActivity.this, "倒計時完成", Toast.LENGTH_SHORT).show();
if (timer != null) {
timer.cancel();
timer = null;
}
}
break;
default:
break;
}
}
;
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_go = (Button) findViewById(R.id.btn_go);
btn_get = (Button) findViewById(R.id.btn_get);
btn_stop = (Button) findViewById(R.id.btn_stop);
et_time = (EditText) findViewById(R.id.et_timer);
tv_time = (TextView) findViewById(R.id.tv_time);
btn_go.setOnClickListener(this);
btn_get.setOnClickListener(this);
btn_stop.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
//獲得倒計時間
case R.id.btn_get:
if (et_time.getText().toString().trim() != null && !(et_time.getText().toString().trim().equals(""))) {
try {
time = Integer.parseInt(et_time.getText().toString());
tv_time.setText(et_time.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "時間格式不正確", Toast.LENGTH_SHORT).show();
return;
}
}
break;
//開始倒計時
case R.id.btn_go:
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
mHandler.sendEmptyMessage(MSG_WHAT);
}
}, 0, 1000);
} else {
Toast.makeText(MainActivity.this, "請勿多次點擊", Toast.LENGTH_SHORT).show();
return;
}
break;
//停止倒計時
case R.id.btn_stop:
if (timer != null) {
timer.cancel();
timer = null;
}
break;
default:
break;
}
}
}
好了,教程到此結束。如果此文章幫到了你,歡迎點贊。
源代碼需要的可自行下載:
(AndroidStudio版)github下載地址:
https://github.com/QQ986945193/DavidTimerDown
(AndroidStudio版)Git開源中國下載地址:
http://git.oschina.net/MCXIAOBING/DavidTimerDown