0

您好,我只是 android 的新手 ..我只是想问一下,我是否可以将 BMI 的计算从菜单窗口显示到另一个名为 android 中的 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);


            }
        });
4

1 回答 1

0

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

您不需要 newActivity就可以更改布局(显示的内容)。只需在方法中做您想做的事情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 中,只有类/接口名称以大写开头。类字段以小写字母开头。

于 2013-01-22T12:17:22.933 回答