获得第一意图总和的价值到第二

时间:2016-01-30 10:55:21

标签: android android-intent

我在编写BMI计算器时遇到了麻烦。

我想在第二个活动中显示已计算的值,但只有当我按下按钮然后直接转到没有值的第二个活动时才会计算该值。

由于

  

主要活动

public class MainActivity extends AppCompatActivity {

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

        final EditText e1 = (EditText) findViewById(R.id.et1);
        final EditText e2 = (EditText) findViewById(R.id.et2);
        final TextView tv4 = (TextView) findViewById(R.id.tv4);


        Button b = (Button) this.findViewById(R.id.button1);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i = new Intent(MainActivity.this, Resultado.class);
                i.putExtra("result1", tv4.getText().toString());
                startActivity(i);

                String str1 = e1.getText().toString();
                String str2 = e2.getText().toString();

                if (TextUtils.isEmpty(str1)) {
                    e1.setError("Insira o peso em Kg");
                    e1.requestFocus();
                    return;
                }

                if (TextUtils.isEmpty(str2)) {
                    e2.setError("Insira a altura em Cm");
                    e2.requestFocus();
                    return;
                }
                //Get the user values from the widget reference
                float weight = Float.parseFloat(str1);
                float height = Float.parseFloat(str2) / 100;


                float bmiValue = calculateBMI(weight, height);


                String bmiInterpretation = interpretBMI(bmiValue);
                tv4.setText(String.valueOf(bmiValue));


            }
        });

    }

    //Calculate BMI
    private float calculateBMI(float weight, float height) {
        return (weight / (height * height));
    }
  

第二个观众

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

        final TextView imc = (TextView) findViewById(R.id.imc);
        Intent i = getIntent();
        String result1 = i.getStringExtra("result1");
        imc.setText("Your name is: " + result1);

        Button back = (Button)this.findViewById(R.id.button2);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}

2 个答案:

答案 0 :(得分:0)

按钮监听器中的

请在计算后调用启动活动。

Button b = (Button) this.findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {


        String str1 = e1.getText().toString();
        String str2 = e2.getText().toString();

        if (TextUtils.isEmpty(str1)) {
            e1.setError("Insira o peso em Kg");
            e1.requestFocus();
            return;
        }

        if (TextUtils.isEmpty(str2)) {
            e2.setError("Insira a altura em Cm");
            e2.requestFocus();
            return;
        }
        //Get the user values from the widget reference
        float weight = Float.parseFloat(str1);
        float height = Float.parseFloat(str2) / 100;


        float bmiValue = calculateBMI(weight, height);


        String bmiInterpretation = interpretBMI(bmiValue);
        tv4.setText(String.valueOf(bmiValue));


        Intent i = new Intent(MainActivity.this, Resultado.class);
        i.putExtra("result1", tv4.getText().toString());
        startActivity(i);
    }
});

答案 1 :(得分:0)

在完成所有计算后开始您的活动。

String bmiInterpretation = interpretBMI(bmiValue);
tv4.setText(String.valueOf(bmiValue));

Intent i = new Intent(MainActivity.this, Resultado.class);
i.putExtra("result1", tv4.getText().toString());
startActivity(i);