在android中显示另一个活动中BMI的计算

时间:2013-01-22 11:24:24

标签: android

你好,我刚刚开始使用android ..我只是想问一下如何在菜单窗口中显示BMI的计算到另一个名为Register Window的活动。因为当我运行此代码时没有显示结果在注册窗口中..谢谢

* 注册窗口*


  public void onClick(View v)
    {   
        Intent intent = new Intent(Register.this, Menu.class);
            intent.putExtra("response","counter");
            startActivity(intent);   

        int weight = 0, height = 0, age = 0, answer = 0;
        String test1, test2, test3;
        test1 = getString(R.id.tvWeight);
        test2 = getString(R.id.tvAge);
        test3 = getString(R.id.tvHeight);

        try {
            if (test1 != "" && test2 != "" && test3 != "") {
                Eweight = (EditText) findViewById(R.id.tvWeight);
                weight = Integer.parseInt(Eweight.getText().toString().trim());
                Eage = (EditText) findViewById(R.id.tvAge);
                age = Integer.parseInt(Eage.getText().toString().trim());
                Eheight = (EditText) findViewById(R.id.tvHeight);
                height = Integer.parseInt(Eheight.getText().toString().trim());

                if(gender.contains("Male"))
                    answer = (int) Math.round(1.2 * (66 + (13.7 * weight) + (5 * height) - (6.8 * age)));

                if(gender.contains("Female"))
                    answer = (int) Math.round(1.2*(655 + (9.6 * weight) + (1.8 * height) - (4.7 * age)));

                response = answer + " kcal/day";
                counter.setText(response);


            }
        } catch (Exception e) {
            System.out.println(e);
        }

    }     

菜单窗口

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent intent = getIntent();
                intent.getStringExtra("response");

                String answer = intent.getStringExtra(response);


            }
        });

1 个答案:

答案 0 :(得分:0)

很难详细回答,因为您没有提供足够的细节,例如什么是gender

您只需要更改布局(显示的内容)就不需要新的Activity。只需在onClick()方法中执行您想要执行的操作(包括布局更改)。

如果您想要以新的Activity进行操作,则需要使用startActivityForResult()。您可以使用Bundle(第三个参数)将您想要的任何内容传递给新的Activity。请注意,当这个新的孩子 Activity结束时,将会调用来电者的onActivityResult(),而不是您在此处相信的代码中的下一句话:

    Intent intent = new Intent(Register.this, Menu.class);
    intent.putExtra("response","counter");
    startActivity(intent);   

    int weight = 0, height = 0, age = 0, answer = 0;
    String test1, test2, test3;
    test1 = getString(R.id.tvWeight);
    test2 = getString(R.id.tvAge);
    test3 = getString(R.id.tvHeight);

PS:在Java中,只有类/接口名称以大写字母开头。类字段以小写字母开头。