Android实现简单倒计时
来源:互联网 发布:java中嵌套循环 编辑:程序博客网 时间:2024/06/07 21:06
界面比较破旧,凑合来看,主要看逻辑实现。(倒计时)
原理:因为防止android 主线程的阻塞,所以使用timer对象的run方法+handler进行UI更新。
附加说明:
obtainMessage和new Message()的区别
1创建方式不同;
2obtainMessage 已经不是 自己创建,是从MessagePool 拿的,省去了创建对象申请内存
废话不多说,直接上代码。
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:background="@drawable/ji"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/Edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入时间" />
<Button
android:id="@+id/dedao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="得到时间" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/start"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="倒计时" />
<Button
android:id="@+id/zstart"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="清零" />
</LinearLayout>
<Button
android:id="@+id/end"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="停止" />
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_below="@+id/button2"
android:layout_marginTop="19dp"
android:layout_weight="0.44"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="hello"
android:textColor="@android:color/background_dark"
android:textSize="100dp" />
</LinearLayout>
逻辑代码:
package com.example.jishiq;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Time;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button start = null;
private Button zstart = null;
private Button end = null;
private Button dedao = null;
private EditText edit = null;
private TextView tv = null;
private Timer time = null;
private TimerTask task = null;
private TimerTask task1 = null;
private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) this.findViewById(R.id.end);
end = (Button) this.findViewById(R.id.start);
edit = (EditText) this.findViewById(R.id.Edit);
tv = (TextView) this.findViewById(R.id.textView);
zstart = (Button) this.findViewById(R.id.zstart);
dedao = (Button) this.findViewById(R.id.dedao);
dedao.setOnClickListener(this);
start.setOnClickListener(this);
end.setOnClickListener(this);
zstart.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.dedao:
tv.setText(edit.getText().toString());
i = Integer.parseInt(edit.getText().toString());
// 创建时间对象
break;
case R.id.start:
// 获取时间
starttime();
break;
case R.id.zstart:
// 获取时间
time.cancel();
tv.setText("0");
break;
case R.id.end:
stoptime();
break;
}
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
tv.setText(msg.arg1 + "");
// tv.setText(msg.arg2+"");
starttime();
}
};
private void starttime() {
time = new Timer();
task = new TimerTask() {
@Override
public void run() {
i--;
Message msg = handler.obtainMessage();
msg.arg1 = i;
handler.sendMessage(msg);
}
};
time.schedule(task, 1000);
}
private void stoptime() {
time.cancel();
}
}
- Android倒计时简单实现
- 简单Android倒计时实现
- Android实现简单倒计时
- Android倒计时简单方法实现
- Android 简单实现倒计时CountDownTimer
- android倒计时的简单实现
- 简单实现Android倒计时功能
- android倒计时的简单实现方法
- Android 4.0以上简单实现倒计时按钮
- Android订单倒计时的简单实现
- Android---验证码倒计时的简单实现
- Android三种超简单办法,实现按钮倒计时
- Android实现验证码倒计时简单效果
- Unity 倒计时简单实现
- 简单倒计时实现
- 简单倒计时逻辑实现
- 倒计时功能简单实现
- 倒计时的简单实现
- MFC框架界面开发(四):文档类
- Linux——时间
- Ubuntu16.04切换默认python版本
- 找点有意思的事情
- 类和对象、访问限定符、默认成员函数
- Android实现简单倒计时
- 统计学习方法(二)感知器C语音实现
- Spring BeanPostProcessor(后处理器)的简单使用
- 两个不同类型的重载
- JSP基础(十二)——初步认识JDBC
- 17年商汤笔试题
- 测试系统没问题,正式系统就有问题的解决思路
- 数据结构与C语言实现(四)——树(中):堆的二叉树实现
- 机器学习笔记:初识sklearn(一)