【发布时间】:2014-11-08 19:01:08
【问题描述】:
我有一个关于 android 应用程序开发的问题正在困扰我。
在我的应用程序中,它完全按照我想要的方式工作,除了一个部分,我无法弄清楚如何在不创建新 Intent 的情况下将一条数据从一个活动发送到另一个活动。
在我的代码中,用户输入他的姓名、质量和身高,当用户点击按钮计算时,它会将新意图中的所有值带到第二个活动中,在那里计算用户的 BMI . 现在,我想在不创建新意图的情况下将这个新计算的 BMI 发送回第一个活动,但我现在确定如何去做
这是我的代码的相关部分
主 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 时,它会将值带到 textView 应该显示的第三个 Activity 例如 "BMI: 20.66" 而是我得到“BMI:null”,我将如何解决这个错误?
【问题讨论】:
-
不要使用任何 SharedPreferences/sdcard 文件等,使用 startActivityForResult() 模式
-
是的,我只是在搜索@Deminem 在他进行编辑之前发布的内容,而我发现的内容对我有用
-
@ProgrammingNewb - 如果这解决了您的问题,请接受帮助社区的答案。编码愉快!
标签: java android android-intent