android 倒计时器 设计
android中如何实现点击一个button然后出现自定义倒计时时间的窗口或小插件,确定后开始倒计时,倒计时的数字任然显示在button上...
android中如何实现 点击一个button然后出现自定义倒计时时间的窗口或小插件,确定后开始倒计时,倒计时的数字任然显示在button上
展开
3个回答
展开全部
有三种实现方式:
第一种:通过Timer与TimerTask结合的方式进行实现
第二种:通过Handler与Message方式进行实现
第三种:通过CountdownTimer进行实现
示例
第一种:
timer.schedule(task, 1000, 1000); // timeTask ,一秒执行一次,
TimerTask task = new TimerTask() {
@Override
public void run() {
}
}
第二种:
handler.sendMessageDelayed(message, 1000); //1秒后发送消息
final Handler handler = new Handler(){
public void handleMessage(Message msg){ // handle message
switch (msg.what) {
case 1:
recLen--;
if(recLen > 0){ //如果倒计时没到
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 1000); // 1秒后发送消息
}
}
super.handleMessage(msg);
}
};
第三种:
new CountdownTimer(30000, 1000) { //30秒倒计时,没秒执行一次
public void onTick(long millisUntilFinished) { //每秒执行一次进该方法
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() { //倒计时结束。
mTextField.setText("done!");
}
}.start();
第一种:通过Timer与TimerTask结合的方式进行实现
第二种:通过Handler与Message方式进行实现
第三种:通过CountdownTimer进行实现
示例
第一种:
timer.schedule(task, 1000, 1000); // timeTask ,一秒执行一次,
TimerTask task = new TimerTask() {
@Override
public void run() {
}
}
第二种:
handler.sendMessageDelayed(message, 1000); //1秒后发送消息
final Handler handler = new Handler(){
public void handleMessage(Message msg){ // handle message
switch (msg.what) {
case 1:
recLen--;
if(recLen > 0){ //如果倒计时没到
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 1000); // 1秒后发送消息
}
}
super.handleMessage(msg);
}
};
第三种:
new CountdownTimer(30000, 1000) { //30秒倒计时,没秒执行一次
public void onTick(long millisUntilFinished) { //每秒执行一次进该方法
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish() { //倒计时结束。
mTextField.setText("done!");
}
}.start();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
展开全部
用timer做一个 计时操作
追问
就是不会呀 求源码或者链接
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
最近做了一个,给你贴代码
Timer timer = new Timer();
static int hour = 0;
static int minute = 0;
static int second = 0;
//给变量赋值(要计时多久)
timerTask = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
timer.schedule(timerTask, 0, 1000);
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
if (hour == 0) {// /////////////////////////////////////////
if (minute == 0) {
if (second == 0) {// ʱ�䵽
timeView.setText("Time out !");
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
} else {// 00:30,00:08
second--;
if (second >= 10) {
timeView.setText("0" + hour + "小时" + "0"
+ minute + "分" + second);
} else {
timeView.setText("0" + hour + "小时" + "0"
+ minute + "分0" + second);
}
}
} else {// minute!=0,33:00->32:59
if (second == 0) {
second = 59;
minute--;
if (minute >= 10) {// 33:30
timeView.setText("0" + hour + "小时" + minute
+ "分" + second);
} else {// 06:30
timeView.setText("0" + hour + "小时" + "0"
+ minute + "分" + second);
}
} else {// 32:30->32:29
second--;
if (second >= 10) {
if (minute >= 10) {
timeView.setText("0" + hour + "小时" + minute
+ ":" + second);
} else {// 09:30
timeView.setText("0" + hour + "小时" + "0"
+ minute + "分" + second);
}
} else {// 12:30
if (minute >= 10) {
timeView.setText("0" + hour + "小时" + minute
+ "分0" + second);
} else {// 08:30
timeView.setText("0" + hour + "小时" + "0"
+ minute + "分0" + second);
}
}
}
}
}
// hour>0
else {
if (minute == 0) {// hour>0,minute=0
if (second == 0) {
minute = 59;
second = 59;
hour--;
if (hour >= 10) {
if (second >= 10) {
timeView.setText(hour + "小时" + minute + "分"
+ second);
} else {
timeView.setText(hour + "小时" + minute + "分0"
+ second);
}
} else {
if (second >= 10) {
timeView.setText("0" + hour + "小时" + minute
+ "分" + second);
} else {
timeView.setText("0" + hour + "小时" + minute
+ "分0" + second);
}
}
} else {
second--;
if (hour >= 10) {
if (second >= 10) {
timeView.setText(hour + "小时0" + minute + "分"
+ second);
} else {
timeView.setText(hour + "小时0" + minute
+ "分0" + second);
}
} else {
if (second >= 10) {
timeView.setText("0" + hour + "小时0" + minute
+ "分" + second);
} else {
timeView.setText("0" + hour + "小时0" + minute
+ "分0" + second);
}
}
}
} else {// hour>0,minute!=0
if (second == 0) {
second = 59;
minute--;
if (hour >= 10) {
if (second >= 10) {
if (minute >= 10) {
timeView.setText(hour + "小时" + minute
+ "分" + second);
} else {
timeView.setText(hour + "小时0" + minute
+ "分" + second);
}
} else {
if (minute >= 10) {
timeView.setText(hour + "小时" + minute
+ "分0" + second);
} else {
timeView.setText(hour + "小时0" + minute
+ "分0" + second);
}
}
} else {
if (second >= 10) {
if (minute >= 10) {
timeView.setText("0" + hour + "小时"
+ minute + ":" + second);
} else {
timeView.setText("0" + hour + "小时0"
+ minute + "分" + second);
}
} else {
if (minute >= 10) {
timeView.setText("0" + hour + "小时"
+ minute + "分0" + second);
} else {
timeView.setText("0" + hour + "小时0"
+ minute + "分0" + second);
}
}
}
} else {
second--;
if (hour >= 10) {
if (second >= 10) {
if (minute >= 10) {
timeView.setText(hour + "小时" + minute
+ "分" + second);
} else {
timeView.setText(hour + "小时0" + minute
+ "分" + second);
}
} else {
if (minute >= 10) {
timeView.setText(hour + "小时" + minute
+ "分0" + second);
} else {
timeView.setText(hour + ":0" + minute
+ "分0" + second);
}
}
} else {
if (second >= 10) {
if (minute >= 10) {
timeView.setText("0" + hour + "小时"
+ minute + "分" + second);
} else {
timeView.setText("0" + hour + "小时0"
+ minute + "分" + second);
}
} else {
if (minute >= 10) {
timeView.setText("0" + hour + "小时"
+ minute + "分0" + second);
} else {
timeView.setText("0" + hour + "小时0"
+ minute + "分0" + second);
}
}
}
}
}
}
}
}
};
Timer timer = new Timer();
static int hour = 0;
static int minute = 0;
static int second = 0;
//给变量赋值(要计时多久)
timerTask = new TimerTask() {
@Override
public void run() {
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
timer.schedule(timerTask, 0, 1000);
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
if (hour == 0) {// /////////////////////////////////////////
if (minute == 0) {
if (second == 0) {// ʱ�䵽
timeView.setText("Time out !");
if (timer != null) {
timer.cancel();
timer = null;
}
if (timerTask != null) {
timerTask = null;
}
} else {// 00:30,00:08
second--;
if (second >= 10) {
timeView.setText("0" + hour + "小时" + "0"
+ minute + "分" + second);
} else {
timeView.setText("0" + hour + "小时" + "0"
+ minute + "分0" + second);
}
}
} else {// minute!=0,33:00->32:59
if (second == 0) {
second = 59;
minute--;
if (minute >= 10) {// 33:30
timeView.setText("0" + hour + "小时" + minute
+ "分" + second);
} else {// 06:30
timeView.setText("0" + hour + "小时" + "0"
+ minute + "分" + second);
}
} else {// 32:30->32:29
second--;
if (second >= 10) {
if (minute >= 10) {
timeView.setText("0" + hour + "小时" + minute
+ ":" + second);
} else {// 09:30
timeView.setText("0" + hour + "小时" + "0"
+ minute + "分" + second);
}
} else {// 12:30
if (minute >= 10) {
timeView.setText("0" + hour + "小时" + minute
+ "分0" + second);
} else {// 08:30
timeView.setText("0" + hour + "小时" + "0"
+ minute + "分0" + second);
}
}
}
}
}
// hour>0
else {
if (minute == 0) {// hour>0,minute=0
if (second == 0) {
minute = 59;
second = 59;
hour--;
if (hour >= 10) {
if (second >= 10) {
timeView.setText(hour + "小时" + minute + "分"
+ second);
} else {
timeView.setText(hour + "小时" + minute + "分0"
+ second);
}
} else {
if (second >= 10) {
timeView.setText("0" + hour + "小时" + minute
+ "分" + second);
} else {
timeView.setText("0" + hour + "小时" + minute
+ "分0" + second);
}
}
} else {
second--;
if (hour >= 10) {
if (second >= 10) {
timeView.setText(hour + "小时0" + minute + "分"
+ second);
} else {
timeView.setText(hour + "小时0" + minute
+ "分0" + second);
}
} else {
if (second >= 10) {
timeView.setText("0" + hour + "小时0" + minute
+ "分" + second);
} else {
timeView.setText("0" + hour + "小时0" + minute
+ "分0" + second);
}
}
}
} else {// hour>0,minute!=0
if (second == 0) {
second = 59;
minute--;
if (hour >= 10) {
if (second >= 10) {
if (minute >= 10) {
timeView.setText(hour + "小时" + minute
+ "分" + second);
} else {
timeView.setText(hour + "小时0" + minute
+ "分" + second);
}
} else {
if (minute >= 10) {
timeView.setText(hour + "小时" + minute
+ "分0" + second);
} else {
timeView.setText(hour + "小时0" + minute
+ "分0" + second);
}
}
} else {
if (second >= 10) {
if (minute >= 10) {
timeView.setText("0" + hour + "小时"
+ minute + ":" + second);
} else {
timeView.setText("0" + hour + "小时0"
+ minute + "分" + second);
}
} else {
if (minute >= 10) {
timeView.setText("0" + hour + "小时"
+ minute + "分0" + second);
} else {
timeView.setText("0" + hour + "小时0"
+ minute + "分0" + second);
}
}
}
} else {
second--;
if (hour >= 10) {
if (second >= 10) {
if (minute >= 10) {
timeView.setText(hour + "小时" + minute
+ "分" + second);
} else {
timeView.setText(hour + "小时0" + minute
+ "分" + second);
}
} else {
if (minute >= 10) {
timeView.setText(hour + "小时" + minute
+ "分0" + second);
} else {
timeView.setText(hour + ":0" + minute
+ "分0" + second);
}
}
} else {
if (second >= 10) {
if (minute >= 10) {
timeView.setText("0" + hour + "小时"
+ minute + "分" + second);
} else {
timeView.setText("0" + hour + "小时0"
+ minute + "分" + second);
}
} else {
if (minute >= 10) {
timeView.setText("0" + hour + "小时"
+ minute + "分0" + second);
} else {
timeView.setText("0" + hour + "小时0"
+ minute + "分0" + second);
}
}
}
}
}
}
}
}
};
追问
计时的时间需要个人定义,也就是说需要一个定时界面,不是在代码里面就写好了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询