【问题标题】:Number Picker calculaiton号码选择器计算
【发布时间】:2016-03-10 12:25:55
【问题描述】:

我已经创建了 3 个不同的数字选择器,我正在使用选择的 3 个不同的数字进行计算,但不知道为什么它不会在 totalP textView 中显示总和。这2行是错的吗? "sum =(a*4)+(b*4)+(c*9); "和 "totalP.setText(sum+" cal");"

    public TextView carb;
    public TextView protein;
    public TextView fat;
    public TextView totalP;
    int carbNum,proteinNum,fatNum,cpv,ppv,fpv;
    int sum,a,b,c;

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

        Bundle extras = getIntent().getExtras();

        carb = (TextView) findViewById(R.id.carbsNum);
        carbNum = extras.getInt("Carb");
        carb.setText(carbNum+" g");

        protein = (TextView) findViewById(R.id.proteinNum);
        proteinNum = extras.getInt("Protein");
        protein.setText(proteinNum+" g");

        fat = (TextView) findViewById(R.id.fatNum);
        fatNum = extras.getInt("Fat");
        fat.setText(fatNum+" g");

        cpv=extras.getInt("carbP");
        ppv=extras.getInt("proteinP");
        fpv=extras.getInt("fatP");

        totalP=(TextView) findViewById(R.id.totalPercentage);

        final NumberPicker np1 = (NumberPicker) findViewById(R.id.carbPercentage);
        final NumberPicker np2 = (NumberPicker) findViewById(R.id.proteinPercentage);
        final NumberPicker np3 = (NumberPicker) findViewById(R.id.fatPercentage);

        np1.setMaxValue(1000);
        np1.setMinValue(0);
        np1.setValue(carbNum);
        np1.setWrapSelectorWheel(true);
        np1.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                a =np1.getValue();;
                carb.setText(a + " g");
            }
        });
        np2.setMaxValue(1000);
        np2.setMinValue(0);
        np2.setValue(proteinNum);
        np2.setWrapSelectorWheel(true);
        np2.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                b = np2.getValue();
                protein.setText(b + " g");

            }
        });
        np3.setMaxValue(1000);
        np3.setMinValue(0);
        np3.setValue(fatNum);
        np3.setWrapSelectorWheel(true);
        np3.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                c = np3.getValue();
                fat.setText(c + " g");
            }
        });

        sum =(a*4)+(b*4)+(c*9);
        // this line cant work, is because the a,b,c ??
        totalP.setText(sum+" cal");  
        // this line cant work  ??
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }
}

【问题讨论】:

  • 你必须让你的方法返回a、b和c的值,然后你把它们和sum放在一起

标签: java android numberpicker android-number-picker


【解决方案1】:

把这两行放在所有改变监听器中,它会起作用, 在 oncreate 函数中,a,b,c 在 setOnValueChangedListener 执行之前没有任何价值,这就是为什么那 2 行不起作用

【讨论】:

  • 我在所有更改侦听器中都复制了第 2 行,它可以工作,但是当我选择第一个数字选择器时,它只显示计算第 2 个和第 3 个它忽略的第一个,我该如何解决它,我想让它和其他2个一起添加,我想把它改成数组吗?
猜你喜欢
  • 2016-03-04
  • 2015-01-31
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 2011-10-25
  • 2018-07-20
  • 2013-11-23
相关资源
最近更新 更多