1

我是java新手,我需要一些帮助。我需要将 bmivalue 变量从 MainActivity 转移到目标类。我研究了其他关于如何做到这一点的帖子,但我不知道在这种情况下如何做到这一点。

package com.example.bmiworking;

import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class goal extends Activity {
    Button btn;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.goal);
        btn = (Button) findViewById(R.id.bmiButton);
        btn.setText(bmiValue);
}
}


package com.example.bmiworking;

import android.app.Activity;
import android.content.DialogInterface;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.homeClickHandler);
    btn.setOnClickListener(this);
}

    @Override
public void onClick(View v) {
    if (v.getId() == R.id.homeClickHandler) {
        startActivity(new Intent(this, MainMenu.class));
    }
}

public void calculateClickHandler(View view) {
    // make sure we handle the click of the calculator button

    if (view.getId() == R.id.calculateButton) {

        // get the references to the widgets
        EditText weightText = (EditText) findViewById(R.id.weightText);
        EditText heightText = (EditText) findViewById(R.id.heightText);
        TextView resultText = (TextView) findViewById(R.id.resultLabel);

        // get the users values from the widget references

        float weight = Float.parseFloat(weightText.getText().toString());
        float height = Float.parseFloat(heightText.getText().toString());

        // calculate the bmi value

        float bmiValue = calculateBMI(weight, height);

        // interpret the meaning of the bmi value
        String bmiInterpretation = interpretBMI(bmiValue);

        // now set the value in the result text

        resultText.setText(bmiValue + "-" + bmiInterpretation);
    }
}

// the formula to calculate the BMI index

// check for http://en.wikipedia.org/wiki/Body_mass_index
private float calculateBMI(float weight, float height) {

    return (float) (weight * 4.88 / (height * height));
}

// interpret what BMI means
private String interpretBMI(float bmiValue) {

    if (bmiValue < 16) {
        return "Severely Underweight - See Weight Gain";
    } else if (bmiValue < 18.5) {

        return "Underweight - See Weight Gain";
    } else if (bmiValue < 25) {

        return "Normal - No Recomendations";
    } else if (bmiValue < 30) {

        return "Overweight - See Weight Loss";
    } else {
        return "Obese - See Weight Loss";
    }

}

}
4

4 回答 4

3

我们可以通过使用将变量和值从一个类传递到另一个类intent。我在酒店项目中使用的示例代码如下。希望对你有帮助

Intent in = new Intent(getApplicationContext(), goal.class);
in.putExtra("h_id", h_id);
in.putExtra("lat1", lat1);
in.putExtra("lon1", lon1);
startActivity(in);

在这里,您可以通过这种方式开始下一个类执行,并将值从当前类传递到goal class使用in.putExtras("varible_name in new class, value ")

并在新的班级目标中使用

Intent in = getIntent();
        h_id = in.getStringExtra("h_id");
        lat1 = in.getDoubleExtra("lat1", 0);
        lon1 = in.getDoubleExtra("lon1", 0);
于 2013-01-24T17:42:11.520 回答
2

您可以从一个意图开始您的目标活动,其中包含一个包含额外值的 Bundle 类型对象,如下所示:

Intent intent = new Intent(this, MainActivity.class);
Bundle extras = new Bundle();
extras.putInt("int-key", int_value);
extras.putString("string-key", string_value);
extras.putFloat("float-key", float_value);
intent.putExtras(extras);
startActivity(intent);

然后在您的goal类中,您可以将此代码放入您的onCreate()方法中以检索值:

Intent receivedIntent = getIntent();
Bundle extras = intent.getExtras();
int int_value = extras.getInt("int-key", 0);
String string_value = extras.getString("string-key", "");
float float_value = extras.getFloat("float-key", 0.0);
于 2013-01-24T17:54:10.433 回答
2

如果这仍然是开放的并且正在被查看,那么您真的应该为此使用共享首选项。 http://developer.android.com/guide/topics/data/data-storage.html#pref

比意图更简单、更高效,并且通过共享首选项,您可以将数据传递给片段和活动,反之亦然

于 2013-07-17T13:07:36.000 回答
1

有几种方法。

我认为使您的数据“全局”可用的一种有用方法是在 Application 的子类中创建数据结构。有一个 Application 对象在任何活动中始终可用。当您扩展 Application 类时,您必须在清单中注意这一点。

您可以检索您的应用程序对象,

  MyApplication myApp = (MyApplication) getApplication();

然后是你的数据结构,

  ArrayList<Thing> myThings = myApp.getMyData();

您可以在任何活动中检索和修改数据结构。我很想知道其他人是否这样做。

于 2013-01-24T17:55:55.537 回答