我目前有两个号码选择器。一种用于设置小时值,另一种用于设置倒数计时器的分钟值。目前,倒数计时器的值是小时数选择器或分钟数选择器的值,但两者都不是应该的。所以我的问题是如何将倒数计时器的textView更改为小时和分钟数字选择器值。如果您有任何疑问,这可能会使您感到困惑。
这是我的代码...

public class MainActivity extends AppCompatActivity {
NumberPicker noPickerHours;
NumberPicker noPickerMinutes;
TextView textViewTime;
Button button;
CounterClass timer;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    textViewTime = (TextView) findViewById(R.id.textView);

    noPickerMinutes = (NumberPicker) findViewById(R.id.numberPicker2);
    noPickerMinutes.setMaxValue(59);
    noPickerMinutes.setMinValue(0);
    noPickerHours = (NumberPicker) findViewById(R.id.numberPicker);
    noPickerHours.setMaxValue(24);
    noPickerHours.setMinValue(0);
    button = (Button)findViewById(R.id.button);



    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            timer.start();
        }
    });

    noPickerHours.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {


        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            timer = new CounterClass((newVal * 60 * 1000), 1000);
            long millis = (newVal * 60 * 1000);
            String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            textViewTime.setText(hms);
        }
    });

    noPickerMinutes.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {


        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
            timer = new CounterClass((newVal * 60 * 1000), 1000);
            long millis = (newVal * 60 * 1000);
            String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
            textViewTime.setText(hms);
        }
    });

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}



public class CounterClass extends CountDownTimer {


    /**
     * @param millisInFuture    The number of millis in the future from the call
     *                          to {@link #start()} until the countdown is done and {@link #onFinish()}
     *                          is called.
     * @param countDownInterval The interval along the way to receive
     *                          {@link #onTick(long)} callbacks.
     */

    public CounterClass(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
    }



    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @Override

    public void onTick(long millisUntilFinished) {
        long millis = millisUntilFinished;
        String hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis), TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
                TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
        System.out.println(hms);
        textViewTime.setText(hms);

    }

    @Override
    public void onFinish() {

    }


}}

最佳答案

您的代码的问题在于,更改值后,在NumberPickernoPickerHoursnoPickerMinutes对象中,都会调用setOnValueChangedListener()方法。如果更改noPickerHours选择器,则计时器设置为小时,如果更改noPickerMinutes选择器,则计时器设置为指定的分钟。因此,您更改的最后一个数字选择器将是计时器的值。

因此解决方案将是:

int hourPicked;
int minPicked;  // declare both as instance variables otherwise will need to declare as final variables.

button.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // put your logic here to set the time with hourPicked and minPicked variables.
        timer.start();
    }
});

noPickerHours.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {


    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
       hourPicked = newVal;
    }
});

noPickerMinutes.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {


    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
         minPicked = newVal;
    }
});


希望这可以帮助。

关于java - 将两个数字选择器的值设置为倒数计时器的小时和分钟,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34247817/

10-10 10:22