如果我更改值,Android倒数计时器将不起作用 [英] Android countdown timer not working if i change values

查看:82
本文介绍了如果我更改值,Android倒数计时器将不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,在第一个活动中输入带有数字选择器的数字,在第二个活动中,存在一个倒数计时器,该计时器使用您在第一个活动中输入的数字以分钟为单位进行倒计时(如果您输入10倒数10分钟)。

I am trying to make a program that you enter a number with numberpicker on the first activity and on the second activity there is a countdown timer that uses the number you entered on the first activity to countdown from in minutes(if you enter 10 it countdown 10 minutes).

public class timer_2 extends AppCompatActivity {
ImageButton imageButton3;
private TextView timer_2_up;
private TextView timer_2_down;
private CountDownTimer timer_2_up_countdowntimer;
private CountDownTimer timer_2_down_countdowntimer;
private boolean timer_2_up_running;
private boolean timer_2_down_running;
private long starttime;
private long starttimedown = starttime*60000;
private long starttimeup = starttime*60000;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer_2);
    timer_2_up = findViewById(R.id.timer_2_up);
    timer_2_down = findViewById(R.id.timer_2_down);
    imageButton3 = (ImageButton)findViewById(R.id.imageButton3);
    Bundle timer2extras = getIntent().getExtras();
    String timer2string = timer2extras.getString("timer2string");
    starttime = Integer.parseInt(timer2string);
    imageButton3.setOnClickListener(new View.OnClickListener() {
      //....                  

如果我同时在startimeup和starttimedown上都删除了starttime * 60000(第9行),并替换为一个数字(例如600000),它可以正常工作,但starttime仅显示00:00。请帮帮我。

If i remove starttime*60000(9th row) both on startimeup and starttimedown and replace with a number (for example 600000) it works fine but with starttime it just shows 00:00.Please help me.

推荐答案

只需在启动时声明starttimeup和starttime并对其进行初始化,然后从第一个活动中获取starttime值即可将它们初始化:

Just declare starttimeup and starttime down in starting and initialise them after you get starttime value from 1st activity like:

long starttimeup;
long starttimedown;

并在onCreate中:

And in onCreate:

starttime = Integer.parseInt(timer2string);
starttimeup = starttime*60000;
starttimedown = starttime*60000;

这篇关于如果我更改值,Android倒数计时器将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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