简体   繁体   中英

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 .

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. If an input value greater than 1000 is given (ex 10 seconds ie 10000), the app shows the count down.

  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(); 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. That is causing "Times up0" observed.

The updated onClick method is as follows.

    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

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();
    
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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