将号码从第一个活动传递到号码选择器

2024-01-04

这是我的第一个活动

public void onClick(View v) {
        switch (v.getId()) {

            case R.id.setButton:

                int cpv= carbPValue;
                int ppv = proteinPValue;
                int fpv=fatPValue;

                Intent intent = new Intent ( this, AdjustMacronutrients.class );
                intent.putExtra ( "carbP", cpv );
                intent.putExtra ( "proteinP", ppv );
                intent.putExtra ( "fatP", fpv );

                startActivity(intent);
                finish();
        }
    }

这是我的第二项活动:

我从第一个活动中传递了 3 个整数值,即cpv, ppv, fpv.

在用户选择号码之前,如何将号码选择器内的这些值设置为默认值。并显示数字TextView用户从 3 个不同的数字选择器中进行选择后,最后将整数值传递回第一个活动。

Bundle extras = getIntent().getExtras();

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

 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);

                int sum = 0;

                for (int i = 0; i < 3; i++) {
                    String[] values = nps[i].getDisplayedValues();
                    sum += Integer.parseInt(values[nps[i].getValue()]);

                }
                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);
        }

您可以使用:

  • NumberPicker.setValue() http://developer.android.com/reference/android/widget/NumberPicker.html#setValue(int)方法来设置默认值。

eg:

nps[0].setValue(cpv);
nps[1].setValue(ppv);
nps[2].setValue(fpv);
  • NumberPicker.getValue() http://developer.android.com/reference/android/widget/NumberPicker.html#getValue()获取值并更新您的文本。

eg:

tv.setText(String.valueOf(nps[0].getValue())); // or just tv.setText(""+nps[0].getValue());
  • Activity.onActivityResult() http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int,%20int,%20android.content.Intent)回调将新值传递给您的第一个活动(您可以在this https://stackoverflow.com/a/2621390/4224337回答)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将号码从第一个活动传递到号码选择器 的相关文章

随机推荐