往后几天应该会看线程的知识
先看一个 倒计时的小Demo
private Handler mHandler = new Handler() {
public void handleMessage(Message mes){
mTvTime.setText(mes.arg1+"");
startTime();
};
};
/**
* 开始计时器
*/
public void startTime(){
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
if (i<=0){
stopTime();
Toast.makeText(getApplicationContext(),"倒计时结束",Toast.LENGTH_SHORT).show();
}else {
i--;
Message message = mHandler.obtainMessage();
message.arg1 = i;
mHandler.sendMessage(message);
}
}
};
timer.schedule(timerTask,1000);
}
/**
* 结束计时器
*/
public void stopTime(){
timer.cancel(); //清除线程
}
这里主要运用的是Timer
既然用到了Timer 那就少不了TimerTask了
Timer 是 Android 机制中提供的一个计时器
还有一个需要注意的是 只能在ui线程中修改ui
下面附上源码
<?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">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_setTime"/>
<Button
android:id="@+id/btn_getTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获得时间"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_time"
android:textSize="18sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始倒计时"/>
<Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结束倒计时"/>
</LinearLayout>
</LinearLayout>
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 android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mBtnGetTime;
private Button mBtnStart;
private Button mBtnStop;
private TextView mTvTime;
private EditText mEtTime;
private int i=0;
private Timer timer = null;
private TimerTask timerTask = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
/**
* 初始化布局
*/
private void initView() {
mBtnGetTime = (Button) findViewById(R.id.btn_getTime);
mBtnStart = (Button) findViewById(R.id.btn_start);
mBtnStop = (Button) findViewById(R.id.btn_stop);
mTvTime = (TextView) findViewById(R.id.tv_time);
mEtTime = (EditText) findViewById(R.id.et_setTime);
mBtnGetTime.setOnClickListener(this);
mBtnStart.setOnClickListener(this);
mBtnStop.setOnClickListener(this);
}
/**
* 点击事件
* @param v
*/
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_getTime:
mTvTime.setText(mEtTime.getText());
i = Integer.parseInt(mTvTime.getText().toString());
break;
case R.id.btn_start:
startTime();
break;
case R.id.btn_stop:
stopTime();
break;
}
}
private Handler mHandler = new Handler() {
public void handleMessage(Message mes){
mTvTime.setText(mes.arg1+"");
startTime();
};
};
/**
* 开始计时器
*/
public void startTime(){
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
if (i<=0){
stopTime();
Toast.makeText(getApplicationContext(),"倒计时结束",Toast.LENGTH_SHORT).show();
}else {
i--;
Message message = mHandler.obtainMessage();
message.arg1 = i;
mHandler.sendMessage(message);
}
}
};
timer.schedule(timerTask,1000);
}
/**
* 结束计时器
*/
public void stopTime(){
timer.cancel();
}
}