将数字从 NumberPicker 发送到另一个 Activity,后者使用该数字作为 CountDownTimer 的开始时间

Sending a number from a NumberPicker to another Activity which uses the number as start time for a CountDownTimer

在第一个 activity 上,我想用号码选择器设置号码,我有这个:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer_settings);
    NumberPicker np = (NumberPicker)findViewById(R.id.numberPicker2);
    np.setMinValue(1);
    np.setMaxValue(999);
    np.setWrapSelectorWheel(false);
    np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            Intent timer2 = new Intent(timer_settings.this, timer_2.class);
            Bundle timer2extras = new Bundle();
            timer2extras.putString("timer2string", newVal + "");
            timer2.putExtras(timer2extras);
        }
    });
    final Button testbutton = (Button) findViewById(R.id.testbutton);
    testbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent testintent = new Intent(timer_settings.this, timer_2.class);
            startActivity(testintent);
        }
    });
}

然后在第二个 activity 我想用这个数字作为倒计时的开始时间,我有这个:

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 Bundle timer2extras = getIntent().getExtras();
private String timer2string = timer2extras.getString("timer2string");
private long starttime = Integer.parseInt(timer2string);




@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);
    imageButton3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            imageButton3.animate().rotation(imageButton3.getRotation()+180).start();
            imageButton3.setEnabled(false);

            Timer buttonTimer = new Timer();
            buttonTimer.schedule(new TimerTask() {

                @Override
                public void run() {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            imageButton3.setEnabled(true);
                        }
                    });
                }
            }, 300);
            if (timer_2_up_running){
                pausetimer_2_up();
                starttimer_2_down();

            }else {
                starttimer_2_up();
                if (timer_2_down_running) {
                    pausetimer_2_down();
                }


            }

        }
    });



}
private void starttimer_2_up() {
    timer_2_up_countdowntimer = new CountDownTimer(starttime, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            starttime = millisUntilFinished;
            update_up_text();


        }

        @Override
        public void onFinish() {
            timer_2_up_running=false;

        }
    }.start();

    timer_2_up_running = true;
}

private void starttimer_2_down() {
    timer_2_down_countdowntimer = new CountDownTimer(starttime, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            starttime = millisUntilFinished;
            update_down_text();

        }

        @Override
        public void onFinish() {
            timer_2_down_running=false;

        }
    }.start();

    timer_2_down_running=true;
}

private void pausetimer_2_up () {
    timer_2_up_countdowntimer.cancel();
    timer_2_up_running=false;
}

private void pausetimer_2_down() {
    timer_2_down_countdowntimer.cancel();
    timer_2_down_running=false;
}

private void update_up_text() {
    int minutes_up = (int) (starttime / 1000) / 60;
    int seconds_up = (int) (starttime / 1000) % 60;
    String time_2_up_left_formatted = String.format(Locale.getDefault(),"%02d:%02d", minutes_up, seconds_up);
    timer_2_up.setText(time_2_up_left_formatted);
}

private  void  update_down_text() {
    int minutes_down = (int) (starttime / 1000) / 60;
    int seconds_down = (int) (starttime / 1000) % 60;
    String time_2_down_left_formatted = String.format(Locale.getDefault(),"%02d:%02d", minutes_down, seconds_down);
    timer_2_down.setText(time_2_down_left_formatted);
}

但它无法显示我无法找到解决方案的空错误。 我不知道这是因为 NumberPicker 和倒数计时器之间的 long/int 差异还是我忘记添加一些行。

请帮帮我。

因为你是把bundle extra放到一个Intent中,然后再启动另一个Intent。您不必关注 NumberPicker 的值更改事件。只要 getValue() 每当你想启动定时器 activity.

在你的第一个activity中,它应该是

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer_settings);
    NumberPicker np = (NumberPicker)findViewById(R.id.numberPicker2);
    np.setMinValue(1);
    np.setMaxValue(999);
    np.setWrapSelectorWheel(false);

    final Button testbutton = (Button) findViewById(R.id.testbutton);
    testbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent testintent = new Intent(timer_settings.this, timer_2.class);
Bundle timer2extras = new Bundle();
            timer2extras.putString("timer2string", np.getValue() + "");
            timer2.putExtras(timer2extras);
            startActivity(testintent);
        }
    });
}

在计时器activity中,当一切准备就绪时,将getIntent().getExtras()的代码放入onCreate()

private Bundle timer2extras;
private String timer2string;
private long starttime;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer_2);

    // Find views 

    timer2extras = getIntent().getExtras();
    timer2string = timer2extras.getString("timer2string");
    starttime = Integer.parseInt(timer2string);

    // ...
}