我已经创建了3个数字选择器,计划在选中时添加/总计这些数字选择器,但现在我面临一些字符串数组问题
输入:这里是我选择数字选择器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);
}
答案 0 :(得分:0)
我不明白为什么你试图通过字符串连接&#39; temp&#39;来连接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));
如果有帮助,请告诉我。
答案 1 :(得分:0)
请参阅下面@oathkeeper
的ans是真的。
让我描述一件最好的事情。
&#34;的 + 强>&#34;它以不同的方式工作。
(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的简单和基本概念。
答案 2 :(得分: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);
}