2

我已经创建了 3 个数字选择器,计划在选择时添加/总计这些数字选择器,但现在我面临一些字符串数组问题

输入:这里我选择的数字 frm number picker 20, 30 , 10

输出:103020

但我需要它总计 10+20+30=60 并且不显示像 103020 这样的输出

   NumberPicker.OnValueChangeListener onValueChanged = new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                NumberPicker[] nps = new NumberPicker[3];
                nps[0] = (NumberPicker) findViewById(R.id.carbPercentage);
                nps[1] = (NumberPicker) findViewById(R.id.proteinPercentage);
                nps[2] = (NumberPicker) findViewById(R.id.fatPercentage);

                String temp = "";
                for (int i = 0; i < 3; i++) {
                    String[] values = nps[i].getDisplayedValues();
                    temp = values[nps[i].getValue()] + temp;
                }

                TextView tv = (TextView) findViewById(R.id.totalPercentage);
                tv.setText(temp);
            }
        };

        NumberPicker[] nps = new NumberPicker[3];
        nps[0] = (NumberPicker) findViewById(R.id.carbPercentage);
        nps[1] = (NumberPicker) findViewById(R.id.proteinPercentage);
        nps[2] = (NumberPicker) findViewById(R.id.fatPercentage);

        String[] values = new String[21];
        for (int i = 0; i < values.length; i++) {
            values[i] = Integer.toString(i * 5);
        }

        for (int i = 0; i < 3; i++) {
            nps[i].setMaxValue(values.length - 1);
            nps[i].setMinValue(0);
            nps[i].setDisplayedValues(values);
            nps[i].setOnValueChangedListener(onValueChanged);
        }
4

3 回答 3

0

看到下面的@oathkeeper 答案是真的。让我描述一件最好的事情。

+ ” 它以不同的方式工作。

(String + string) always return string concate.
(String + int/long) always return string concate.
(int/long + string) always return string concate.
(int/long + int/long) always return int/long (Mathamatical opration).

简单和基本的 JAVA 概念。

于 2015-12-01T05:18:39.207 回答
0

您只需要更改nps[i].getDisplayedValues()nps[i].getValue(),它会返回一个int值。然后将这些int值加在一起,然后显示出来。

   NumberPicker.OnValueChangeListener onValueChanged = new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                NumberPicker[] nps = new NumberPicker[3];
                nps[0] = (NumberPicker) findViewById(R.id.carbPercentage);
                nps[1] = (NumberPicker) findViewById(R.id.proteinPercentage);
                nps[2] = (NumberPicker) findViewById(R.id.fatPercentage);

                // change here!
                int sum = 0;
                // String temp = "";
                for (int i = 0; i < 3; i++) {
                    String[] values = nps[i].getDisplayedValues();
                    sum += Integer.parseInt(values[nps[i].getValue()]);
                    // temp = values[nps[i].getValue()] + temp;
                }

                TextView tv = (TextView) findViewById(R.id.totalPercentage);
                tv.setText(String.valueOf(sum));
            }
        };

        NumberPicker[] nps = new NumberPicker[3];
        nps[0] = (NumberPicker) findViewById(R.id.carbPercentage);
        nps[1] = (NumberPicker) findViewById(R.id.proteinPercentage);
        nps[2] = (NumberPicker) findViewById(R.id.fatPercentage);

        String[] values = new String[21];
        for (int i = 0; i < values.length; i++) {
            values[i] = Integer.toString(i * 5);
        }

        for (int i = 0; i < 3; i++) {
            nps[i].setMaxValue(values.length - 1);
            nps[i].setMinValue(0);
            nps[i].setDisplayedValues(values);
            nps[i].setOnValueChangedListener(onValueChanged);
        }

于 2015-12-01T05:25:35.367 回答
0

我不明白你为什么试图通过'temp' 变量的字符串连接来连接 NumberPicker 值。而是使用这个:

在数字选择器的 onValueChange 事件处理程序中,

long totalSum = 0L;

for(int i=0; i<3; i++)
      totalSum + = nps[i].getValue(); // getValue() method returns int

TextView tv = (TextView) findViewById(R.id.totalPercentage);
tv.setText(String.valueOf(totalSum));

让我知道这是否有帮助。

于 2015-12-01T05:04:59.407 回答