为了账号安全,请及时绑定邮箱和手机立即绑定

倒计时器的 EditText 值

倒计时器的 EditText 值

呼唤远方 2023-07-19 16:34:18
我正在尝试分配用户输入到编辑文本中的值作为倒计时器的起始值,将其转换为毫秒     @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() 方法输出
查看完整描述

2 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

我检查了你的代码,看起来不错,请参阅:


    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: 9

2019-09-09 00:07:00.004 1258-1258/? I/HwAlarmManagerService: hwSetAlarm listenerTag: time_tick

2019-09-09 00:07:00.741 26157-26157/com.unest.android.dev I/alarm rings in: 8

2019-09-09 00:07:01.742 26157-26157/com.unest.android.dev I/alarm rings in: 7

2019-09-09 00:07:02.742 26157-26157/com.unest.android.dev I/alarm rings in: 6

2019-09-09 00:07:03.743 26157-26157/com.unest.android.dev I/alarm rings in: 5

2019-09-09 00:07:04.744 26157-26157/com.unest.android.dev I/alarm rings in: 4

2019-09-09 00:07:05.745 26157-26157/com.unest.android.dev I/alarm rings in: 3

2019-09-09 00:07:06.746 26157-26157/com.unest.android.dev I/alarm rings in: 2

2019-09-09 00:07:07.748 26157-26157/com.unest.android.dev I/alarm rings in: 1

2019-09-09 00:07:08.749 26157-26157/com.unest.android.dev I/alarm rings in: 0

2019-09-09 00:07:09.232 26157-26157/com.unest.android.dev I/alarm done: alarm is ringing

2019-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.gms

2019-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: 1000

2019-09-09 00:07:12.411 1258-1258/? I/HwAlarmManagerService: hwSetAlarm listenerTag: *job.delay*

可能你设定了 1 秒,但终点被解雇了。尝试增加 edittext 中的秒数或设置 1000 毫秒以上的不同间隔


查看完整回答
反对 回复 2023-07-19
?
猛跑小猪

TA贡献1858条经验 获得超8个赞

根据CountDownTimer 类的javadoc(参见其构造函数),millisInFuture 参数的值小于 countDownInterval 参数的值。所以onFinish是在onTick之前执行的。



查看完整回答
反对 回复 2023-07-19
  • 2 回答
  • 0 关注
  • 116 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信