【问题标题】:Creating a stopwatch timer in Android在 Android 中创建秒表计时器
【发布时间】:2013-09-09 04:11:18
【问题描述】:

我已经创建了布局,这是它的代码,但我不知道应该如何更改 mainactivity.java 以及应该添加到 string.xml 文件中的内容:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

      <TextView
        android:id="@+id/androidtimer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Android Timer"
        android:textSize="40sp"
        android:textStyle="bold"
        android:textColor="#FF0000"/>

    <LinearLayout
        android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal"
     android:gravity="bottom|center" >
    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text=" start "
        android:textSize="35sp"
        android:textColor="#FFFFFF"
        android:onClick="startTimer"
/>
    <View
        android:layout_width="20dp"
        android:layout_height="1dp"/>
       <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text=" stop "
        android:textSize="35sp"
        android:textColor="#FFFFFF"
        android:onClick="stopTimer"

    />
           </LinearLayout>
    </LinearLayout>

如果您能给我一些继续的建议,那就太好了。我是 Android 移动开发新手,找不到秒表计时器的分步教程。

【问题讨论】:

  • 将所有文本值添加到 strings.xml,然后引用它们。您的文本大小可以放在您的dimens.xml 中。不知道您所说的更改主要活动是什么意思。
  • @BobbyDigital 谢谢。说你在哪里添加 public void stopTimer(View view) { timerTask.cancel(); timerTask=null; n=0; } 你不把它添加到 MainActivity.java 吗?我应该在哪里为每个按钮添加 onClick 方法?

标签: java android eclipse android-layout sdk


【解决方案1】:

您可以做的只是编写如下代码。

boolean isCounterON = true;
while(isCounterON){
 if(isStart){
    Thread.sleep(1000);
   //update your view to 1 Second 
    }
}

 onStartButtonClick(){
 isStart=true;
 }

 onStopButtonClick(){
 isStart=false;
 }

就是这样

【讨论】:

  • 这段代码应该写在MainActivity.java中吗?另外如何将 onStartButtonClick 与开始按钮的名称联系起来?就像在 Visual Studio 中,你双击一个按钮,你会被引导到它的方法,你可以在那里编写你的代码,但我有点困惑,我可以在 Android SDK 中编写我的方法。
  • 如果您的 xml 名称是 string.xml,则转到 string.java 并在其上实施 actionListener。只需研究 ACTIONLISTENER 代码并将其添加到您的文件中。它只有 2 行代码。
【解决方案2】:

我在 Java 中使用TimerTimerTasks 创建了一个自定义计时器。我已经在这段代码中添加了 cmets,所以它是不言自明的,我希望这对你有帮助。此代码包含工作逻辑,您可以将其用于任何您想要的视图。

代码

import java.util.Timer;
import java.util.TimerTask;

public class StopWatch {
    private long time_counter = 45;  // Seconds to set the Countdown from
    private Timer timer;           

    public void startCountDown(){
        timer = new Timer();        // A thread of execution is instantiated
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println((--time_counter));

                if(time_counter == 0){
                    timer.cancel(); // timer is cancelled when time reaches 0
                }
            }
        },0,1000); 
                    // 0 is the time in second from when this code is to be executed
                    // 1000 is time in millisecond after which it has to repeat
    }


    public static void main(String[] args){
        StopWatch s = new StopWatch();
        s.startCountDown();
    }
}

【讨论】:

    猜你喜欢
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多