用户输入和定时器(Java的Andr​​oid应用程序) [英] User input and timer (java android app)

查看:174
本文介绍了用户输入和定时器(Java的Andr​​oid应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图使像一个秒表计时器,但我一个完整的小白。我试着从相结合的东西在这里这里

我们的目标是把用户输入他们想要多久设置定时器,那么当时间到了它的东西。

这是我迄今为止:

 包com.example.timer;进口android.app.Activity;
进口android.os.Bundle;
进口android.os.CountDownTimer;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;公共类MainActivity延伸活动{
私人CountDownTimer countDownTimer;
私人布尔timerHasStarted = FALSE;
公众的TextView文本;
私人最终长间隔= 1 * 1000;
的EditText editTime1;
按钮startButton;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    editTime1 =(EditText上)findViewById(R.id.editTime1);
    startButton =(按钮)findViewById(R.id.startButton);
    文字=(TextView的)this.findViewById(R.id.timer);
    startButton.setOnClickListener(新OnClickListener(){        公共无效的onClick(视图v){
            //得到的EditText的名称和存储到字符串变量
            长的timeval =的Long.parseLong(editTime1.getText()的toString());
            countDownTimer =新MyCountDownTimer(timeval中,间隔);
            text.setText(text.getText()+将String.valueOf(timeval中/ 1000));            如果(!timerHasStarted){
                   countDownTimer.start();
                   timerHasStarted = TRUE;
                   startButton.setText(STOP);
                  }其他{
                   countDownTimer.cancel();
                   timerHasStarted = FALSE;
                   startButton.setText(RESTART);
                  }
            }
        类MyCountDownTimer扩展CountDownTimer {
          公共MyCountDownTimer(长timeval中,长间隔){
           超(timeval中,间隔);
          }          @覆盖
          公共无效onTick(长millisUntilFinished){
           text.setText(+ millisUntilFinished / 1000);
          }
          @覆盖
          公共无效onFinish(){
           text.setText(泰晤士报了);
          }
         }
        });
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}
}


解决方案

以下几点需要注意。


  1. 该活动预计开始时间是在毫秒。如果输入值大于1000被赋予(前10秒即10000),应用程序显示向下计数。


  2. 下面两行放置不当。

      countDownTimer =新MyCountDownTimer(timeval中,间隔);
    text.setText(text.getText()+将String.valueOf(timeval中/ 1000));

    他们应该只在倒计时开始执行。但是,在给定的实施,他们可以停止运行无论是在起步为好。


其结果是,当倒计时创建一个新MyCountDownTimer被停止,并且c​​ountDownTimer.cancel();被称为在这个新的对象,而不是原来的对象。因此倒计时仍在继续。

自的setText上开始和停止进行两者,所述的timeval被附加到输出。这是造成泰晤士报登录或者观察到。

更新的onClick方法如下。

 公共无效的onClick(视图v){
        //得到的EditText的名称和存储到字符串变量
        长的timeval =的Long.parseLong(editTime1.getText()的toString());        如果(!timerHasStarted){
            countDownTimer =新MyCountDownTimer(timeval中,间隔);
            text.setText(text.getText()+将String.valueOf(timeval中/ 1000));
            countDownTimer.start();
            timerHasStarted = TRUE;
            startButton.setText(STOP);
        }其他{
            countDownTimer.cancel();
            timerHasStarted = FALSE;
            startButton.setText(RESTART);
        }
    }

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 i.e. 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");
        }
    }

这篇关于用户输入和定时器(Java的Andr​​oid应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆