我有一个关于 Android 应用程序开发的问题正在困扰着我。
在我的应用程序中,它完全按照我想要的方式工作,除了我无法弄清楚的一部分是如何在不创建新 Intent 的情况下将数据从一个 Activity 发送到另一个 Activity。
在我的代码中,用户输入他的名字、体重和高度,当用户点击 Button calculate 时,它将新 Intent 中的所有值带到第二个 Activity ,在那里,它计算用户的 BMI . 现在,我想在不创建新 Intent 的情况下将这个新计算的 BMI 发送回第一个 Activity ,但我现在确定如何去做
这是我的代码的相关部分
主 Activity.java
package mobileapp.melvin.bmicalculator;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.*;
import android.content.*;
import android.widget.*;
public class MainActivity extends Activity {
public String name,mass,height,bmi;
public EditText nameField, massField, heightField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bmi = getIntent().getStringExtra("BMI");
//Create "TextFields" By getting ID of Editviews from Main XML
nameField = (EditText) findViewById(R.id.mText_box1);
massField = (EditText) findViewById(R.id.mText_box2);
heightField = (EditText) findViewById(R.id.mText_box3);
//Button To calculate and display BMI as TextViews
Button launchBtn = (Button) findViewById(R.id.mButton_calculate);
launchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Check is The "Textfields" have values
if(!verifyData()){
return;
}
/*Create a new Intent to Launch another activity
* To display all the Values gotten from
* The TextFields as Normal Text Values
*/
Intent launcher = new Intent(v.getContext(),BMI1.class);
//This intent then passes these values over to the next Intent
launcher.putExtra("Name", name);
launcher.putExtra("Mass", mass);
launcher.putExtra("Height", height);
//We then start this new activity with the new Intent
startActivity(launcher);
}
});
}
和BMI1.java
package mobileapp.melvin.bmicalculator;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class BMI1 extends Activity {
String name,mass,height,bmi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi1);
//Get data from the first activity through intent
name = getIntent().getStringExtra("Name");
mass = getIntent().getStringExtra("Mass");
height = getIntent().getStringExtra("Height");
//convert mass and height to double and calculate BMI
double m = Double.parseDouble(mass);
double h = Double.parseDouble(height);
bmi = Double.toString(calculateBMI(m, h));
((TextView) findViewById(R.id.b1_Label2)).setText(name);
((TextView) findViewById(R.id.b1_Label4)).setText(mass);
((TextView) findViewById(R.id.b1_Label6)).setText(height);
((TextView) findViewById(R.id.b1_Label8)).setText(bmi);
Button backBtn = (Button) findViewById(R.id.b1Button_back);
backBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent launcher = getIntent();
launcher.putExtra("BMI", bmi);
finish();
}
});
}
private double calculateBMI(double toMass, double toHeight){
double value;
value = toMass/(toHeight * toHeight);
return value;
}
}
我知道没有传递任何值,因为当用户在第一个 Activity 中单击 Display 时,它会将值带到第三个 Activity,其中 textView 应该显示例如 "BMI: 20.66" 但相反我收到 “BMI: null”,我该如何解决这个错误?
最佳答案
您不必总是使用 Intent 在 Activity 之间发送数据。您使用其他 android 存储选项,如 Sqlite db、SharedPreferences。您还可以将数据存储在 SD 卡上。查看 Android 存储选项 here
关于java - 在不创建新 Intent Android 的情况下将数据从一个 Activity 发送到另一个 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26820900/