您的位置:首页 > 其它

验证码 timer timertask handler

2015-11-06 14:34 399 查看
Ui代码

<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=".MainActivity">

<EditText
android:id="@+id/input_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入倒计时的时间"
android:inputType="number" />

<Button
android:id="@+id/gettime"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:layout_marginTop="10dp"
android:text="获得时间" />

<Button
android:id="@+id/starttime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="开始计时" />

<Button
android:id="@+id/stoptime"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="暂停计时" />

<TextView
android:id="@+id/showtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</LinearLayout>


java代码:

package com.example.administrator.countdown;

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 input_time;
private Button gettime, starttime, stoptime;
private TextView showtime;
private int i = 0;
//计时相关
private Timer timer;
private TimerTask timerTask;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initviews();
}

private void initviews() {
input_time = (EditText) findViewById(R.id.input_time);
gettime = (Button) findViewById(R.id.gettime);
starttime = (Button) findViewById(R.id.starttime);
stoptime = (Button) findViewById(R.id.stoptime);
showtime = (TextView) findViewById(R.id.showtime);
gettime.setOnClickListener(this);
starttime.setOnClickListener(this);
stoptime.setOnClickListener(this);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.gettime:
showtime.setText(input_time.getText().toString());
i = Integer.parseInt(showtime.getText().toString());
break;

case R.id.starttime:

starttime();

break;

case R.id.stoptime:
stoptime();

break;
}
}

private void stoptime() {
//暂停计时
timer.cancel();
}

//handler接收消息
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
showtime.setText(msg.arg1 + "");
starttime();
}
};

private void starttime() {
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
i--;
Message message = mHandler.obtainMessage();
message.arg1 = i;
mHandler.sendMessage(message);
}
};
timer.schedule(timerTask, 1000);  //timer计时器延迟1000毫秒(即多长时间更新一次数据)
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: