倒计时
来源:互联网 发布:大数据定义和概念 编辑:程序博客网 时间:2024/06/05 19:03
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context="com.jikexueyuan.counttime.MainActivity" >
<EditText
android:id="@+id/inputtime"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/gettime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取倒计时时间" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/starttime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始计时" />
<Button
android:id="@+id/stoptime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止计时" />
</LinearLayout>
Activity:
public class MainActivity extends Activity implements OnClickListener{
private EditText inputet;
private Button getTime,startTime,stopTime;
private TextView time;
private int i = 0;
private Timer timer = null;
private TimerTask task = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
inputet = (EditText) findViewById(R.id.inputtime);
getTime = (Button) findViewById(R.id.gettime);
startTime = (Button) findViewById(R.id.starttime);
stopTime = (Button) findViewById(R.id.stoptime);
time = (TextView) findViewById(R.id.time);
getTime.setOnClickListener(this);
startTime.setOnClickListener(this);
stopTime.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.gettime:
time.setText(inputet.getText().toString());
i = Integer.parseInt(inputet.getText().toString());
break;
case R.id.starttime:
startTime();
break;
case R.id.stoptime:
stopTime();
break;
}
}
private Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
time.setText(msg.arg1+"");
startTime();
};
};
public void startTime(){
timer = new Timer();
task = new TimerTask() {
@Override
public void run() {
i--;
Message message = mHandler.obtainMessage();
message.arg1 = i;
mHandler.sendMessage(message);
}
};
timer.schedule(task, 1000);
}
public void stopTime(){
timer.cancel();
}
}
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时、、、、、
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- 倒计时
- Java Number & Math 类
- ueditor使用
- 一个老鸟发的公司内部整理的 Android 学习路线图
- ArcGIS for Android 离线数据空间查询--I查询
- classpath和classpath*的区别
- 倒计时
- 关于jeecg自动生成代码后,进入前端页面后后端报错的问题
- 深度学习笔记---基于动量的梯度下降
- java中静态String的最大长度由什么决定的?
- 简单跨域请求和带预检的跨域请求
- 配置文件读取工具类
- PHP RabbitMQ 实践第二步
- 106_容器_自己实现HashMap_MyMap原始版_效率较低
- 字符串的左旋和右旋