简体   繁体   English

如何在Android开发中将变量从一个类传递到另一个类

[英]How to pass a variable from one class to another in Android development

i am new to java and i need some help. 我是Java的新手,我需要一些帮助。 I need to transfer the bmivalue variable from the MainActivity to the goal class. 我需要将bmivalue变量从MainActivity转移到目标类。 I have researched other posts on how to do this but i cant figure out how to do it in this situation. 我已经研究了其他有关如何执行此操作的帖子,但是在这种情况下我无法弄清楚如何执行此操作。

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

}

}

We can pass the variables and values from one class to another through the use of intent . 我们可以通过使用intent将变量和值从一类传递到另一类。 The sample code I have used in my hotels project is as below. 我在酒店项目中使用的示例代码如下。 hope it will help you 希望对你有帮助

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

here in this way you can start next class execution with intent and passing values from present class to goal class with use of in.putExtras("varible_name in new class, value ") 在这种情况下,您可以使用in.putExtras("varible_name in new class, value ")in.putExtras("varible_name in new class, value ")用意图开始下一个类的执行,并将值从当前类传递到goal class

and in the new class goal use 并在新课程目标中使用

Intent in = getIntent();
        h_id = in.getStringExtra("h_id");
        lat1 = in.getDoubleExtra("lat1", 0);
        lon1 = in.getDoubleExtra("lon1", 0);

You can start your goal activity with an intent, that has a Bundle-type object of extra values in it, like this: 您可以以一个意图开始您的目标活动,该意图活动中包含一个具有附加值的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);

Then inside your goal class you can put this code into your onCreate() method to retrieve the values: 然后,在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);

If this is Still open and being looked at you really should be using shared preferences for this. 如果仍然打开并正在查看,那么您确实应该为此使用共享的首选项。 http://developer.android.com/guide/topics/data/data-storage.html#pref http://developer.android.com/guide/topics/data/data-storage.html#pref

simpler and more efficient than intents and with shared prefs you can pass data to fragments as well as activities and vice versa 比意图更简单,更有效,并且可以使用共享首选项将数据传递给片段以及活动,反之亦然

There are a few ways. 有几种方法。

I think a useful way to make your data available 'globally' is to create the data structures in a sub class of Application. 我认为使数据“全局”可用的一种有用方法是在Application的子类中创建数据结构。 There is one Application object that is always available in any activity. 在任何活动中始终都有一个Application对象。 When you extend the Application class, you must note that in the manifest. 扩展Application类时,必须在清单中注意这一点。

You can retrieve your application object, 您可以检索您的应用程序对象,

  MyApplication myApp = (MyApplication) getApplication();

and then your data structure, 然后是您的数据结构,

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

You can retrieve and modify the data structure in any activity. 您可以在任何活动中检索和修改数据结构。 I'd be interested to know if this is how others are doing this. 我想知道这是否是其他人的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android中将变量从一个类传递到另一个类 - How do I pass a variable from one Class to another in Android 如何将stringarraylist值从一个类传递到android中的另一个类 - how to pass stringarraylist values from one class to another class in android 如何在同一活动android中将变量从一种方法传递到另一种方法 - How to pass a variable from one method to another in the same activity android 如何在android中将arraylist变量从一个类调用到另一个类 - How to call the arraylist variable from one class to another class in android android将变量从一种方法传递到另一种方法 - android pass variable from one method to another 如何在Android开发中将Java方法从一个类调用到另一个类? - How do I call a Java methods from one class to another class in Android development? 如何将变量从一个活动传递到另一个活动? - How to pass variable from one activity to another? 如何将信息从一个 class 传递到另一个 - How to pass information from one class to another Android - 通过textview将文本从一个类传递到另一个类 - Android - pass text from one class to another class via textview 如何将位图从一个坐标移动到另一个坐标-Android开发 - How to move a bitmap from one coordinate to another - Android development
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM