Android SDK (Eclipse):如何为一个简单的应用程序创建一个简单的计时器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20211228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:21:26  来源:igfitidea点击:

Android SDK (Eclipse) : How to create a simple timer for a simple App?

androideclipsetimesdktimer

提问by Souls Reaper

i have a simple Application about writing the sentence which is shown when the application starts.The only Problem is, i need the application to calculate the time it took the user to write the sentence .. like when you touch "Submit" Button , the Toast message will say " Thats Right ! , It took you 3.2 Second" As Example .

我有一个关于编写应用程序启动时显示的句子的简单应用程序。唯一的问题是,我需要应用程序来计算用户编写句子所花费的时间..就像当你触摸“提交”按钮时, Toast 消息会说“就是这样!,它花了你 3.2 秒”作为示例。

I heard you can set a timer to start on when specific action occurs ... and you can order it to stop .

我听说您可以设置一个计时器以在特定操作发生时开始……您可以命令它停止。

So let's say The Timer will start when you start the app and it will stop when you touch "Submit" button , and give a toast message like above calculating the exact time it took you to write the sentience after starting the App ! *

因此,假设计时器将在您启动应用程序时启动,并在您触摸“提交”按钮时停止,并给出如上所示的吐司消息,计算启动应用程序后您编写感知所需的确切时间!*

Here is the App Code hope it helps : *

这是应用程序代码希望它有帮助:*

Button w;

TextView t;

EditText e;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);



w = (Button) findViewById(R.id.Write);

t= (TextView) findViewById(R.id.FTS);

e = (EditText) findViewById(R.id.Text);


w.setOnClickListener(new View.OnClickListener() {

@Override

 public void onClick(View v) {


 String check1 = t.getText().toString();
 String check2 = e.getText().toString();

 if (check1.equals(check2))

     Toast.makeText(MainActivity.this,"You Wrote it Right !!!",Toast.LENGTH_LONG).show();

 else if (check2.equals(""))

Toast.makeText(MainActivity.this,"It's Empty",Toast.LENGTH_LONG).show();

 else 
     Toast.makeText(MainActivity.this,"You wrote it wrong,try again !",Toast.LENGTH_LONG);

I'm Totally new to Android so I really don't know how to do it , Thanks for your Time .*

我对 Android 完全陌生,所以我真的不知道该怎么做,谢谢您的时间。*

回答by Coderji

you can use the Timerclass to start a timer session. Follow the steps:

您可以使用Timer该类来启动计时器会话。按照步骤:

1- define a global variable of the Timerand a variable to count the time like:

1-定义一个全局变量Timer和一个变量来计算时间,如:

private Timer t;
private int TimeCounter = 0;

2- then when the activity starts, so in onCreateadd the following: P.S: what I did is I have a textView to show the timing while he is writing the sentence. so if you dont want that you can delete the tvTimerpart in the following code

2-然后当活动开始时,onCreate添加以下内容:PS:我所做的是我有一个 textView 来显示他写句子时的时间。所以如果你不想,你可以删除tvTimer以下代码中的部分

t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable() {
                public void run() {
                    tvTimer.setText(String.valueOf(TimeCounter)); // you can set it to a textView to show it to the user to see the time passing while he is writing.
                    TimeCounter++;
                }
            });

        }
    }, 1000, 1000); // 1000 means start from 1 sec, and the second 1000 is do the loop each 1 sec.

then when the button is clicked, stop the timing and show the timeCountervaraible in the Toast.

然后当按钮被点击时,停止计时并timeCounterToast.

t.cancel();//stopping the timer when ready to stop.
Toast.makeText(this, "The time taken is "+ String.valueOf(TimeCounter), Toast.LENGTH_LONG).show();

P.S: You have to deal with converting the seconds into minutes because the number could extends to 360 secs so you need to convert it to 6 minutes. you can do it in the t.schedualeAtFixedRateor after you done you can convert it and show it in the toast

PS:您必须处理将秒转换为分钟的问题,因为该数字可能会扩展到 360 秒,因此您需要将其转换为 6 分钟。您可以在完成时t.schedualeAtFixedRate或完成后进行转换,然后将其显示在吐司中

hope you found this useful. please give me a feedback if it worked for you.

希望你觉得这很有用。如果它对你有用,请给我一个反馈。

回答by Jordan Hochstetler

Let me direct your attention to the Chronometer Widget on the Dev Page

让我将您的注意力引向开发页面上的 Chronometer 小部件

Also here's a flavor of what you will get by using the Chronometer Widget (skip to 8:30)

此外,这里还介绍了使用 Chronometer Widget 将获得的体验(跳至 8:30)

Video of Chronometer Widget

天文台小工具的视频

XML

XML

  <Chronometer
    android:id="@+id/chronometer1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

Java

爪哇

((Chronometer) findViewById(R.id.chronometer1)).start();
((Chronometer) findViewById(R.id.chronometer1)).stop();