简体   繁体   English

用户输入和计时器(java android 应用程序)

[英]User input and timer (java android app)

So I'm trying to make a timer like a stopwatch but I'm a complete noob.所以我正在尝试制作一个像秒表一样的计时器,但我是一个完全的菜鸟。 I tried "combining" things from Here and Here .我尝试“组合”来自HereHere 的东西。

The goal is to take user input for how long they want to set the timer for, then when the time is up it does stuff.目标是让用户输入他们想要设置计时器多长时间,然后当时间到时它会做一些事情。

This is what I have so far:这是我到目前为止:

package com.example.timer;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
public TextView text;
private final long interval = 1 * 1000;
EditText editTime1;
Button startButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editTime1 = (EditText)findViewById(R.id.editTime1);     
    startButton = (Button)findViewById(R.id.startButton);
    text = (TextView) this.findViewById(R.id.timer);
    startButton.setOnClickListener(new OnClickListener() { 

        public void onClick(View v) {
            //get the name from edittext and storing into string variable
            long timeVal = Long.parseLong(editTime1.getText().toString());
            countDownTimer = new MyCountDownTimer(timeVal, interval);
            text.setText(text.getText() + String.valueOf(timeVal / 1000));

            if (!timerHasStarted) {
                   countDownTimer.start();
                   timerHasStarted = true;
                   startButton.setText("STOP");
                  } else {
                   countDownTimer.cancel();
                   timerHasStarted = false;
                   startButton.setText("RESTART");
                  }
            }
        class MyCountDownTimer extends CountDownTimer {
          public MyCountDownTimer(long timeVal, long interval) {
           super(timeVal, interval);
          }

          @Override
          public void onTick(long millisUntilFinished) {
           text.setText("" + millisUntilFinished / 1000);
          }
          @Override
          public void onFinish() {
           text.setText("Times up");
          }
         }
        });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

Few things to note.要注意的事情很少。

  1. The Activity expects the start time to be in milliseconds. Activity 预计开始时间以毫秒为单位。 If an input value greater than 1000 is given (ex 10 seconds ie 10000), the app shows the count down.如果给出的输入值大于 1000(例如 10 秒,即 10000),应用程序会显示倒计时。

  2. The following two lines are placed incorrectly.以下两行放置不正确。

     countDownTimer = new MyCountDownTimer(timeVal, interval); text.setText(text.getText() + String.valueOf(timeVal / 1000));

    They should be executed only when the count down is started.它们应该只在倒计时开始时执行。 But in the given implementation, they are run both at start as well as stop.但是在给定的实现中,它们在启动和停止时都运行。

As a result, a new MyCountDownTimer is created when the count down is stopped, and the countDownTimer.cancel();结果,当倒计时停止时,会创建一个新的 MyCountDownTimer,countDownTimer.cancel(); is called in this new object rather than the original object.在这个新对象而不是原始对象中调用。 So the count down continues.于是倒计时继续。

Since the setText is performed both on start and stop, the timeVal is appended to the output.由于 setText 在开始和停止时都执行,因此 timeVal 会附加到输出中。 That is causing "Times up0" observed.这导致观察到“Times up0”。

The updated onClick method is as follows.更新后的 onClick 方法如下。

    public void onClick(View v) {
        // get the name from edittext and storing into string variable
        long timeVal = Long.parseLong(editTime1.getText().toString());

        if (!timerHasStarted) {
            countDownTimer = new MyCountDownTimer(timeVal, interval);
            text.setText(text.getText() + String.valueOf(timeVal / 1000));
            countDownTimer.start();
            timerHasStarted = true;
            startButton.setText("STOP");
        } else {
            countDownTimer.cancel();
            timerHasStarted = false;
            startButton.setText("RESTART");
        }
    }

I found a way to do that.我找到了一种方法来做到这一点。

basically I let user to input their own values.基本上我让用户输入他们自己的值。

layout : HH:MM:SS布局 : HH:MM:SS

then I get these values from layout then process like below :然后我从布局中获取这些值,然后处理如下:

EditText tv_hour = findViewById(R.id.timerHHValue);
        EditText tv_minute = findViewById(R.id.timerMMValue);
        EditText tv_second = findViewById(R.id.timerSSValue);

     long inputTime = TimeUnit.MINUTES.toMillis(minute) + TimeUnit.HOURS.toMillis(hour) + TimeUnit.SECONDS.toMillis(second);
    
            mCountDownTimer = new CountDownTimer(inputTime, 1000) {
                public void onTick(long millisUntilFinished) {
    
                    DecimalFormat f = new DecimalFormat("00");
                    long hours = (millisUntilFinished / 3600000) % 24;
    
                    long minutes = (millisUntilFinished / 60000) % 60;
    
                    long second = (millisUntilFinished / 1000) % 60;
    
                    tv_hour.setText(f.format(hours)+"");
                    tv_minute.setText(f.format(minutes)+"");
                    tv_second.setText(f.format(second)+"");
                }
    
    
                @Override
                public void onFinish() {
                    //    timerFinish.setVisibility(View.VISIBLE);
                    tv_hour.setText("00");
                    tv_minute.setText("00");
                    tv_second.setText("00");
    
    
                }
            }.start();
    
        }

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

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