【发布时间】:2015-03-05 13:44:43
【问题描述】:
我的应用程序允许用户生成卡路里值(主要活动 2)并将其传递给主要活动以便可以显示,当尝试添加多餐时,它不会添加先前的值并传递新值有意图并将其显示在主要活动中,为什么要这样做?任何帮助将不胜感激,解释更是如此。
主要活动
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView calories = (TextView)findViewById(R.id.overall);
String previouscalories = calories.getText().toString();
int numold = Integer.parseInt(previouscalories);
Intent intent = getIntent();
String newcalories = "";
if (intent.hasExtra("passedvalue"))
{
newcalories = intent.getExtras().getString("passedvalue");
} else {
newcalories = "0";
}
int numnew = Integer.parseInt(newcalories);
int overall = numnew + numold;
calories.setText("Current Calories:"+ overall);
Button add = (Button)findViewById(R.id.add_meal);
Button reset = (Button)findViewById(R.id.reset);
Button about = (Button)findViewById(R.id.about);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity2.class);
startActivity(intent);
}
});
about.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),about.class);
startActivity(intent);
}
});
}
}
主要活动2
public class MainActivity2 extends ActionBarActivity {
int sub_weight = 0;
EditText weight;
TextView calories;
Button display, save;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
weight = (EditText)findViewById(R.id.edit_weight);
calories = (TextView)findViewById(R.id.cal_total);
display = (Button)findViewById(R.id.display);
Button save = (Button) findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView calories = (TextView)findViewById(R.id.cal_total);
String g = calories.getText().toString();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("passedvalue", g );
startActivity(intent);
}
});
}
public void onRadioButtonClicked(View v){
boolean checked = ((RadioButton) v).isChecked();
switch (v.getId()) {
case R.id.radiopork:
if (checked)
sub_weight = sub_weight + 2;
break;
case R.id.radiochicken:
if (checked)
sub_weight = sub_weight + 7;
break;
case R.id.radiobeef:
if (checked)
sub_weight = sub_weight + 9;
break;
case R.id.radiosalmon:
if (checked)
sub_weight = sub_weight + 13;
break;
case R.id.radiocod:
if (checked)
sub_weight = sub_weight + 17;
break;
case R.id.radiocereal:
if (checked)
sub_weight = sub_weight + 18;
break;
case R.id.radioporridge:
if (checked)
sub_weight = sub_weight + 23;
break;
case R.id.radiotoast:
if (checked)
sub_weight = sub_weight + 26;
break;
case R.id.radiocrisps:
if (checked)
sub_weight = sub_weight + 29;
break;
case R.id.radionoodle:
if (checked)
sub_weight = sub_weight + 33;
break;
}
}
public void display_calories(View v){
String m = weight.getText().toString();
int x = Integer.parseInt(m);
int y = x * sub_weight;
calories.setText(y+"");
sub_weight = 0;
}
}
【问题讨论】:
标签: android android-intent android-studio