Ⅰ 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,綜合了前幾種方法的優點,是最簡的