Ⅰ android中怎样设置倒计时
你好,
首先需要在输入框中输入一个秒数,比如:12,然后获取输入框中的值,显示在一个TextView中;点击“开始倒计时”按钮,每隔一秒钟,秒数减1,然后显示在TextView中;点击“停止倒计时”按钮,计时器停止运行,当再次点击“开始倒计时”按钮,计时器继续运行。
Ⅱ android 实现同时多个倒计时效果怎么做
安卓实现同时多个倒计时效果的步骤:
我们首先需要在输入框中输入一个秒数,比如:12,然后获取输入框中的值,显示在一个TextView中;
点击“开始倒计时”按钮,每隔一秒钟,秒数减1,然后显示在TextView中;
点击“停止倒计时”按钮,计时器停止运行,当再次点击“开始倒计时”按钮,计时器继续运行。
平时用的每个商品都应该有两个属性值,这里叫它deadLine和isOvere吧,表示商品的到期时间和是否到期,那么倒计时可以考虑这么实现:设置一个主计时器,每隔1秒发出消息告诉GridView现在的时间,GridView的Item获得这个时间之后就和deadLine比较,如果已经到deadline了,就把数据源中的这项标记成已到期(修改isOvere的值为true);没到deadline,就计算还差多少时间,显示到GridView的Item里。
Ⅲ Android倒计时怎么实现
方法一:
利用java的类Timer,TimerTask还有android的Handler
界面welcome_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<TextView
android:id="@+id/count_down"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:gravity="center"
android:textSize="32sp"
android:textColor="#50000000"
android:background="@drawable/count_down_background"
tools:text="2"
/>
</RelativeLayout>
package com.example.counttimer;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.TextView;
public class WelcomeActivity extends Activity {
private final static int COUNT = 1;
private TextView countDown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_welcome);
initView();
}
//sehele的第而个参数是第一次启动延时的时间,第三个是每隔多长时间执行一次。单位都是ms。//因此这里是每一秒发送一次消息给handler更新UI。//然后三秒后时间到了,在timer的第二个sehele中进行跳转到另外一个界面
private void initView() {
countDown = (TextView) findViewById(R.id.count_down);
final Timer timer = new Timer();
final long end = System.currentTimeMillis() + 1000*3;
timer.schele(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(COUNT);
}
}, 0, 1000);
//这里的schele的第二个参数意义是到了这个时间尽快运行run里面的方法
timer.schele(new TimerTask() {
@Override
public void run() {
Intent i = new Intent(WelcomeActivity.this, SecondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
timer.cancel();
}
}, new Date(end));
}
private Handler handler = new Handler(){
int num = 2;
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case COUNT:
countDown.setText(String.valueOf(num));
num--;
break;
default:
break;
}
};
};
}
count_down_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="8dp"/>
<solid
android:color="#1e000000"/>
</shape>
方法二 :
利用android封装的类CountDownTimer。其实内部也是用Handler实现的。其他都一样。
package com.example.counttimer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Window;
import android.widget.TextView;
public class WelcomeActivity extends Activity {
private final static int COUNT = 1;
private TextView countDown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_welcome);
initView();
}
private void initView() {
countDown = (TextView) findViewById(R.id.count_down);//CountDownTimer构造器的两个参数分别是第一个参数表示总时间,第二个参数表示间隔时间。//意思就是每隔xxx会回调一次方法onTick,然后xxx之后会回调onFinish方法。
CountDownTimer timer = new CountDownTimer(3200,1000) {
int num = 2;
@Override
public void onTick(long millisUntilFinished) {
countDown.setText(String.valueOf(num));
num--;
}
@Override
public void onFinish() {//计时完成调用
Intent i = new Intent(WelcomeActivity.this, SecondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();
}
};
timer.start();
}
}
Ⅳ android中怎么实现倒计时怎么按照小时分钟
Android实现计时与倒计时的五种方法:
方法一
Handler与Message(不用TimerTask)
public class timerTask extends Activity{
private int recLen = 11;
private TextView txtView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);
Message message = handler.obtainMessage(1); // Message
handler.sendMessageDelayed(message, 1000);
}
final Handler handler = new Handler(){
public void handleMessage(Message msg){ // handle message
switch (msg.what) {
case 1:
recLen--;
txtView.setText("" + recLen);
if(recLen > 0){
Message message = handler.obtainMessage(1);
handler.sendMessageDelayed(message, 1000); // send message
}else{
txtView.setVisibility(View.GONE);
}
}
super.handleMessage(msg);
}
};
}
linux
方法二
Handler与Thread(不占用UI线程)
public class timerTask extends Activity{
private int recLen = 0;
private TextView txtView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);
new Thread(new MyThread()).start(); // start thread
}
final Handler handler = new Handler(){ // handle
public void handleMessage(Message msg){
switch (msg.what) {
case 1:
recLen++;
txtView.setText("" + recLen);
}
super.handleMessage(msg);
}
};
public class MyThread implements Runnable{ // thread
@Override
public void run(){
while(true){
try{
Thread.sleep(1000); // sleep 1000ms
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}catch (Exception e) {
}
}
}
}
}
方法三
Timer与TimerTask(Java实现)
public class timerTask extends Activity{
private int recLen = 11;
private TextView txtView;
Timer timer = new Timer();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);
timer.schele(task, 1000, 1000); // timeTask
}
TimerTask task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() { // UI thread
@Override
public void run() {
recLen--;
txtView.setText(""+recLen);
if(recLen < 0){
timer.cancel();
txtView.setVisibility(View.GONE);
}
}
});
}
};
}
方法四
TimerTask与Handler(不用Timer的改进型)
public class timerTask extends Activity{
private int recLen = 11;
private TextView txtView;
Timer timer = new Timer();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);
timer.schele(task, 1000, 1000); // timeTask
}
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what) {
case 1:
txtView.setText(""+recLen);
if(recLen < 0){
timer.cancel();
txtView.setVisibility(View.GONE);
}
}
}
};
TimerTask task = new TimerTask() {
@Override
public void run() {
recLen--;
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
}
方法五
Handler与Runnable(最简单型)
public class timerTask extends Activity{
private int recLen = 0;
private TextView txtView;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.timertask);
txtView = (TextView)findViewById(R.id.txttime);
handler.postDelayed(runnable, 1000);
}
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
recLen++;
txtView.setText("" + recLen);
handler.postDelayed(this, 1000);
}
};
}
计时与倒计时
方法1,方法2和方法3,都是倒计时
方法4,方法5,都是计时
计时和倒计时,都可使用上述方法实现(代码稍加改动)
UI线程比较
方法1,方法2和方法3,都是在UI线程实现的计时;
方法4和方法5,是另开Runnable线程实现计时
实现方式比较
方法1,采用的是Java实现,即Timer和TimerTask方式;
其它四种方法,都采用了Handler消息处理
推荐使用
如果对UI线程交互要求不很高,可以选择方法2和方法3
如果考虑到UI线程阻塞,严重影响到用户体验,推荐使用方法4,另起线程单独用于计时和其它的逻辑处理
方法5,综合了前几种方法的优点,是最简的