Android倒计时程序(Timer and TimerTask) - GavinYin
Android倒计时程序(Timer and TimerTask)
简单的倒计时程序—CountTime
Timer 和 TimerTask 实现
<?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:id="@+id/activity_main"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gavin.counttime.MainActivity">
<EditText
android:id="@+id/InputTime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"/>
<Button
android:id="@+id/GetTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Get_Time"/>
<TextView
android:id="@+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/Begin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Begin"/>
<Button
android:id="@+id/Stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Stop"/>
</LinearLayout>
<resources>
<string name="app_name">CountTime</string>
<string name="Get_Time">获取倒计时时间</string>
<string name="Begin">开始计时</string>
<string name="Stop">停止计时</string>
</resources>
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 mInputText;
private Button mGetTime,mStartTime,mStopTime;
private TextView mtime;
private int mSecond;
private Timer mTimer = null;
private TimerTask mTimerTask = null; //两个timer必须配合使用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
mInputText = (EditText)findViewById(R.id.InputTime);
mGetTime = (Button)findViewById(R.id.GetTime);
mStartTime = (Button)findViewById(R.id.Begin);
mStopTime = (Button)findViewById(R.id.Stop);
mtime = (TextView)findViewById(R.id.Time);
mGetTime.setOnClickListener(this);
mStartTime.setOnClickListener(this);
mStopTime.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch (v.getId()) {
case R.id.GetTime:
mtime.setText(mInputText.getText().toString());
mSecond = Integer.parseInt(mInputText.getText().toString());
break;
case R.id.Begin:
StartTime();
break;
case R.id.Stop:
StopTime();
break;
}
}
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
mtime.setText(msg.arg1+" ");
StartTime();
if(msg.arg1 == 0) {
mtime.setText("计时结束");
StopTime();
}
};
};
public void StartTime() {
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
mSecond--;
Message message = mHandler.obtainMessage();
message.arg1 = mSecond;
mHandler.sendMessage(message);
}
};
mTimer.schedule(mTimerTask,1000);
}
public void StopTime() {
mTimer.cancel();
}
}