倒计时器的 EditText 值

我正在尝试分配用户输入到编辑文本中的值作为倒计时器的起始值,将其转换为毫秒


     @Override

    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    minutes = (EditText) findViewById(R.id.minutes);

    seconds = (EditText) findViewById(R.id.seconds);

    start = (Button) findViewById(R.id.startbutton);

    count = (TextView) findViewById(R.id.counting);


    final String sec = String.valueOf(seconds.getText());

    int sec1 = Integer.valueOf(sec);

    long sec2 = sec1;

    //String minu = minutes.getText().toString();


     final CountDownTimer counting = new 

       CountDownTimer(TimeUnit.SECONDS.toMillis(sec2), 1000) {

           @Override

           public void onTick(long millisUntilFinished) {

               Log.i("alarm rings in",                      

                   String.valueOf(millisUntilFinished/1000));

           }


           @Override

            public void onFinish() {


                Log.i("done", "alarm is ringing");

            }

        };


    start.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            counting.start();

        }


    });

}

}


我期望单击按钮后开始倒计时,但它显示 Onfinish() 方法输出


呼唤远方
浏览 117回答 2
2回答

喵喔喔

我检查了你的代码,看起来不错,请参阅:    val sec2 = 10L    val counting: CountDownTimer =        object : CountDownTimer(TimeUnit.SECONDS.toMillis(sec2), 1000) {            override fun onTick(millisUntilFinished: Long) {                Log.i("alarm rings in", (millisUntilFinished / 1000).toString())            }            override fun onFinish() {                Log.i("alarm done", "alarm is ringing")            }        }    counting.start()2019-09-09 00:06:57.791 1258-1654/? I/HwAlarmManagerService: hwSetAlarm listenerTag: *job.delay*2019-09-09 00:06:59.740 26157-26157/com.unest.android.dev I/alarm rings in: 92019-09-09 00:07:00.004 1258-1258/? I/HwAlarmManagerService: hwSetAlarm listenerTag: time_tick2019-09-09 00:07:00.741 26157-26157/com.unest.android.dev I/alarm rings in: 82019-09-09 00:07:01.742 26157-26157/com.unest.android.dev I/alarm rings in: 72019-09-09 00:07:02.742 26157-26157/com.unest.android.dev I/alarm rings in: 62019-09-09 00:07:03.743 26157-26157/com.unest.android.dev I/alarm rings in: 52019-09-09 00:07:04.744 26157-26157/com.unest.android.dev I/alarm rings in: 42019-09-09 00:07:05.745 26157-26157/com.unest.android.dev I/alarm rings in: 32019-09-09 00:07:06.746 26157-26157/com.unest.android.dev I/alarm rings in: 22019-09-09 00:07:07.748 26157-26157/com.unest.android.dev I/alarm rings in: 12019-09-09 00:07:08.749 26157-26157/com.unest.android.dev I/alarm rings in: 02019-09-09 00:07:09.232 26157-26157/com.unest.android.dev I/alarm done: alarm is ringing2019-09-09 00:07:12.378 1258-1379/? W/AlarmManager: mIsScreenOn is: true, WAKEUP alarm trigger action = com.google.android.location.reporting.ACTION_UPDATE_WORLD package name is: com.google.android.gms2019-09-09 00:07:12.378 1258-1379/? W/AlarmManager: mIsScreenOn is: true, WAKEUP alarm talarm.operation == null,package name is: android listenerTag is: *job.delay* creator uid is: 10002019-09-09 00:07:12.411 1258-1258/? I/HwAlarmManagerService: hwSetAlarm listenerTag: *job.delay*可能你设定了 1 秒,但终点被解雇了。尝试增加 edittext 中的秒数或设置 1000 毫秒以上的不同间隔

猛跑小猪

根据CountDownTimer 类的javadoc(参见其构造函数),millisInFuture 参数的值小于 countDownInterval 参数的值。所以onFinish是在onTick之前执行的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java