简体   繁体   English

显示数独在Android中播放的时间

[英]display time played for sudoku in android

I need to display the time ticking a sudoku game has been played, the time played must be displayed on the screen. 我需要显示打数独游戏的时间,必须在屏幕上显示播放的时间。 This means i should be able to record the time from when the sudoku activity has been started till the game fineshes. 这意味着我应该能够记录从数独活动开始到游戏结束的时间。 can anyone help me how to do this in android. 谁能帮助我如何在android中做到这一点。

I use this code every time I need to display time elapsed in a game. 每当我需要显示游戏中经过的时间时,我都会使用此代码。 First you have this TextView for showing your time, defined in your xml, nothing special about it. 首先,您有一个TextView用于显示您的时间(在xml中定义),对此没有什么特别的。 The code in your game activity is: 游戏活动中的代码为:

    TextView time;

    long countUp;

    public static Chronometer.OnChronometerTickListener chronListener = new Chronometer.OnChronometerTickListener() {
        String msg;

        @Override
        public void onChronometerTick(Chronometer ch){
            countUp = (SystemClock.elapsedRealtime() - ch.getBase()) / 1000;
            msg = showTime(countUp);
            time.setText("Time: " + msg);
        }           
    };


//a function formatting your time in human readable format, e.g. 05:06
    public static String showTime(long countUp){
        String asText;
        if((countUp % 60) < 10){
            if((countUp / 60) < 10){
                asText = "0" + (countUp / 60) + ":0" + (countUp % 60); 
            } else {
                asText = (countUp / 60) + ":0" + (countUp % 60); 
            }
        } else {
            if ((countUp / 60) < 10){
                asText = "0" + (countUp / 60) + ":" + (countUp % 60);
            } else {
                asText = (countUp / 60) + ":" + (countUp % 60); 
            }
        }
        return asText;
    }

It works like a charm. 它像一种魅力。

I would suggest looking into a Handler and the postDelayed method. 我建议研究Handler和postDelayed方法。 This is a great option for second intervals, but it tends to choke up at like 25 milliseconds in my very brief experience. 对于第二个间隔,这是一个不错的选择,但根据我的简短经验,它通常会在25毫秒左右停顿。 http://developer.android.com/reference/android/os/Handler.html#postDelayed%28java.lang.Runnable,%20long%29 http://developer.android.com/reference/android/os/Handler.html#postDelayed%28java.lang.Runnable,%20long%29

Then make a runnable that changes a variable of time, and updates a TextView in your app with the new time. 然后,使可运行项更改时间变量,并使用新时间更新应用程序中的TextView。 It will have to be formatted as well. 它也必须被格式化。 http://developer.android.com/reference/java/lang/Runnable.html http://developer.android.com/reference/java/lang/Runnable.html

This should help you grasp the idea and has some example code: http://www.vogella.de/articles/AndroidPerformance/article.html 这应该可以帮助您理解这个想法,并提供一些示例代码: http : //www.vogella.de/articles/AndroidPerformance/article.html

To implement it, you will need a way to determine if the user has won, and when they do, you need to remove the callbacks from the Handler, and then you will have the total time in your variable (use a long) holding the number of milliseconds they have played. 要实现它,您将需要一种方法来确定用户是否获胜,以及何时获得用户,您需要从Handler中删除回调,然后将总时间(长)保存在变量中(他们播放的毫秒数。

Feel free to comment with questions and hopefully I can help ya out. 随意提出问题,并希望我能帮助您。 It took me a while to figure out how to do these things so I'm happy to point you in the right direction if you get stuck. 我花了一段时间才弄清楚如何做这些事情,因此如果您遇到困难,很高兴为您指出正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM