2014-10-11 186 views
0

我做了一个简单的应用程序,其中包含一个测验问题和用户选择一个答案,但我需要你的帮助,在我的应用程序中添加一个倒计时器20秒,当这个时间到了,它将直接转移到下一个问题,在一次用户回答也将转移到下一个问题倒数计时器?

谢谢

+0

http://www.androidhub4you.com/2014/01/countdown-example-in-android-custom.html – 2014-10-11 21:55:40

+0

Android有一个内置的类为此http://developer.android.com/reference/android /os/CountDownTimer.html – Simon 2014-10-11 23:02:01

回答

1
import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.Timer; 
import javax.swing.WindowConstants; 


public class Countdown extends JFrame { 

    // Countdown 42 seconds 
    public static int counterValue = 42; 
    public static Timer timer; 
    public static JLabel label; 

    public Countdown() { 
    initGUI(); 
    } 

    private void initGUI(){ 
    BorderLayout thisLayout = new BorderLayout(); 
    this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 
    this.getContentPane().setLayout(thisLayout); 
    label = new JLabel(); 
    label.setText(String.valueOf(counterValue)); 
    this.getContentPane().add(label, BorderLayout.CENTER); 
    this.setTitle("Countdown Example"); 
     this.pack(); 
     this.setVisible(true); 
    } 


    public static void main(String[] args) { 
    Countdown countdown = new Countdown(); 


    Countdown.timer = new Timer(1000, new ActionListener() { 

     public void actionPerformed(ActionEvent e) { 
     // =1 sec 
     Countdown.counterValue--; 

     Countdown.label.setText(String.valueOf(counterValue)); 


     if(Countdown.counterValue == 0){ 
      System.out.println("Counterdown ausgelaufen!"); 

      // Timer stop 
      Countdown.timer.stop(); 
     } 
     } 
    }); 

    // Timer start 
    timer.start(); 
    } 
} 

http://blog.mynotiz.de/programmieren/java-countdown-und-timer-am-beispiel-von-swing-1707/两者(德国)

0

的“Android的方式”做定时的事情是通过张贴Runnable任务到Handler