我正在使用android。我正在创建一个移动应用程序,需要根据体重指数,年龄和体力活动给出1型糖尿病卡路里摄入量的建议。我的问题是我的if else语句无法正常工作。如果其他编码在Review.java,我需要在Result.java上显示输出。Android;如果其他语句不起作用
Bmi.java
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
// Find UI elements by ID and save to variable
final EditText height = (EditText) findViewById(R.id.height_input);
final EditText weight = (EditText) findViewById(R.id.weight_input);
final TextView bmi_result = (TextView) findViewById(R.id.bmi_result);
Button button = (Button) findViewById(R.id.bmi_calc_button);
Button next = (Button) findViewById(R.id.NavigateButton);
// Listen for our click event
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//String bmi = bmi_result.getText().toString();
// Check for null
if (height.getText().toString().length() < 1) {
sendToast("You must enter your height!");
// Abort the onClick if null
return;
}
if (weight.getText().toString().length() < 1) {
sendToast("You must enter your weight, sorry!");
// Abort the onClick if null
return;
}
// Passed the null checks, let's do some math!
/***
* Android is funny this way, but you have
* to convert it back and forth from integer/float
* to strings, you'll get used to it. ;)
*/
// Convert height from string to float
float h = Float.valueOf(height.getText().toString());
float w = Float.valueOf(weight.getText().toString());
/***
* Time for math!
* BMI is calculated
* (weigth in kg/(height in meter * height in meter)
* But since we want the user to input in CM, we just
* multiply it with 10 000 to get the correct value.
*/
float BMI = w/(h * h) * 10000;
// Set the bmi_result view item of the value of our BMI
bmi_result.setText(String.valueOf(BMI));
String textBMI = bmi_result.getText().toString();
}
});
next.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v){
String textBMI = bmi_result.getText().toString();
Intent intent = new Intent(Bmi.this, Age.class);
intent.putExtra("Ans_bmi",textBMI);
startActivity(intent);
}
});
}
Age.java
private RadioGroup age;
//private RadioButton radioAgeButton;
private Button b;
public String selectedType= "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.age);
age = (RadioGroup) findViewById(R.id.radioGroup);
final RadioButton rb1 = (RadioButton) findViewById(R.id.rb1);
final RadioButton rb2 = (RadioButton) findViewById(R.id.rb2);
b = (Button) findViewById(R.id.button);
age.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.rb1) {
selectedType = rb1.getText().toString();
Toast.makeText(Age.this, "Your age is around 41 - 45", Toast.LENGTH_LONG).show();
}
else if (checkedId == R.id.rb2) {
selectedType = rb2.getText().toString();
Toast.makeText(Age.this, "Your age is around 46 - 50", Toast.LENGTH_LONG).show();
}//int selectedId = age.getCheckedRadioButtonId();
//radioAgeButton = (RadioButton) findViewById(selectedId);
//Toast.makeText(Age.this, radioAgeButton.getText(), Toast.LENGTH_SHORT).show();
}
});
Intent intent = getIntent();
final String textBMI = intent.getStringExtra("Ans_bmi");
b.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(Age.this, Physical_Activity.class);
Bundle bundle = new Bundle();
bundle.putString("Ans_bmi", textBMI);
bundle.putString("Ans_age", selectedType);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
Physical_Activity.java
private RadioGroup activity;
//private RadioButton radioActivityButton;
private String selectedType = "";
private Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.physical_activity);
Intent intent = getIntent();
final String textBMI = intent.getStringExtra("Ans_bmi");
final String umur = intent.getStringExtra("Ans_age");
final TextView cf_result = (TextView) findViewById(R.id.show_result);
activity = (RadioGroup) findViewById(R.id.radioGroup);
final RadioButton rbs = (RadioButton) findViewById(R.id.rbsedentary);
final RadioButton rbm = (RadioButton) findViewById(R.id.rbmoderate);
final RadioButton rba = (RadioButton) findViewById(R.id.rbactive);
b = (Button) findViewById(R.id.button);
activity.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.rbsedentary) {
selectedType = rbs.getText().toString();
Toast.makeText(Physical_Activity.this, "Your physical activity is Sedentary Active", Toast.LENGTH_LONG).show();
} else if (checkedId == R.id.rbmoderate) {
selectedType = rbm.getText().toString();
Toast.makeText(Physical_Activity.this, "Your physical activity is Moderately Active", Toast.LENGTH_LONG).show();
} else if (checkedId == R.id.rbactive) {
selectedType = rba.getText().toString();
Toast.makeText(Physical_Activity.this, "Your physical activity is Active", Toast.LENGTH_LONG).show();
}
//int selectedId = activity.getCheckedRadioButtonId();
//radioActivityButton = (RadioButton)findViewById(selectedId);
//Toast.makeText(Physical_Activity.this, radioActivityButton.getText(), Toast.LENGTH_SHORT).show();
}
});
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Physical_Activity.this, Review.class);
Bundle bundle = new Bundle();
bundle.putString("Ans_bmi", textBMI);
bundle.putString("Ans_age", umur);
bundle.putString("Ans_phy", selectedType);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
Review.java
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.review);
//Create a bundle object to store the bundle added to the intent
final Bundle bundle = getIntent().getExtras();
//Get the values out by key
final String textBMI = bundle.getString("Ans_bmi");
final String umur = bundle.getString("Ans_age");
final String aktiviti = bundle.getString("Ans_phy");
final String txtcf = bundle.getString("cfDisease");
//Get the textview controls
final TextView txtage = (TextView) findViewById(R.id.textView5);
final TextView txtphy = (TextView) findViewById(R.id.textView6);
final TextView txtbmi = (TextView) findViewById(R.id.textView7);
final TextView cf_result = (TextView) findViewById(R.id.result);
//Button buttonSend = (Button) findViewById(R.id.btnsend);
//Set the text values of the text controls
txtage.setText(textBMI);
txtphy.setText(umur);
txtbmi.setText(aktiviti);
cf_result.setText(txtcf);
Button submit = (Button) findViewById(R.id.btnsend);
final Intent intentsubmit = new Intent();
intentsubmit.setClass(Review.this, Result.class);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(intentsubmit);
String calories;
if (txtage.toString().equals(context.getString(R.string.age1)))
{
if (txtphy.toString().equals(context.getString(R.string.moderate)))
{
calories = "1800 cal";
cf_result.setText(String.valueOf(calories));
String txtcf = cf_result.getText().toString();
Intent intent = new Intent(Review.this, Result.class);
intentsubmit.setClass(Review.this, Result.class);
intentsubmit.putExtra("cfDisease", txtcf);
startActivity(intentsubmit);
}
}
else if (txtage.toString().equals(context.getString(R.string.age2)))
{
if (txtphy.toString().equals(context.getString(R.string.active)))
{
calories = "2000 cal";
cf_result.setText(String.valueOf(calories));
String txtcf = cf_result.getText().toString();
Intent intent = new Intent(Review.this, Result.class);
intentsubmit.setClass(Review.this, Result.class);
intentsubmit.putExtra("cfDisease", txtcf);
startActivity(intentsubmit);
}
}
}
});
}
Result.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Intent intent = getIntent();
final String txtcf = intent.getStringExtra("cfDisease");
final TextView keputusan = (TextView) findViewById(R.id.show_result);
keputusan.setText(txtcf);
}
'我的问题是我的输出没有working.'你这是什么意思呢? – njzk2
我的输出将是“卡路里”。但输出没有出现 –
你得到了什么输出?请解释发生了什么。 – APC