验证码 timer timertask handler
2015-11-06 14:34
399 查看
Ui代码
java代码:
<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毫秒(即多长时间更新一次数据) } }
相关文章推荐
- 神奇的自我修复手机膜,贴膜界从此消失了!
- git基本操作总结
- apache2.2.25+mod_jk-apache-2.2.2.so+apache-tomcat-7.0.56集群
- poj1328/2109/2586 贪心算法
- Android自定义权限怎样设置?
- Activity启动从底部向上滑动出现,关闭的时候从顶部向下滑动消失的动画实现---Android提高篇
- js区分中英文统计字符个数
- 字节序
- android studio自动生成ID插件
- iOS NSDate 各种时间字符说明
- Spring、Bean 的作用域
- JVM程序员的阅读清单
- MAVEN(1)环境搭建
- UDP 广播发包失败
- leetcode Binary Tree Right Side
- ogre 不常用类记录之Serializer
- ruby 自带的全局变量
- TortoiseSVN客户端重新设置用户名和密码
- leetcode@ [84/85] Largest Rectangle in Histogram & Maximal Rectangle
- Spring、Bean 的作用域