我正在尝试制作一个程序,您在第一个活动中输入带有numberpicker的数字,而在第二个活动中有一个倒数计时器,它使用您在第一个活动中输入的数字,以分钟为单位进行倒计时(如果您输入10它倒计时10分钟。)
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)它可以正常工作但是在启动时它只显示00:00。请帮助我。
答案 0 :(得分:4)
在启动初始化时启动starttimeup和starttime并在从第1个活动获得starttime值后初始化它们,如:
long starttimeup;
long starttimedown;
在onCreate:
starttime = Integer.parseInt(timer2string);
starttimeup = starttime*60000;
starttimedown = starttime*60000;