我已经创建了 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);
}