我希望得到一些帮助(对 android 来说很新)。
我目前有4个activity。前三个activity中有EditText字段,然后最后一个activity显示了到目前为止输入的所有信息。看起来很简单,对吧?
所以在查看这个网站后,我发现这样做的方法是使用 putExtra() 和 getExtra();
这似乎不太有效,因为我添加到额外哈希集中的所有东西在最终活动中都不可用。有什么建议吗?
谢谢
下面的代码寻求帮助
package com.example.smallchangebigloss;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class EnterName extends Activity {
private EditText edit;
private Bundle extras = getIntent().getExtras();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_name);
edit = (EditText) findViewById(R.id.nameEdit);
}
public Bundle getExtras(){
return extras;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.enter_name, menu);
return true;
}
public void next(View view) {
if (edit.getText().toString().equals("")) {
new AlertDialog.Builder(this).setTitle("Ut Oh!")
.setMessage("Please enter your name.")
.setNeutralButton("Try again", null).show();
}
else {
Intent i = new Intent(getApplicationContext(), CurrentWeight.class);
i.putExtra("name", edit.getText().toString());
startActivity(i);
}
}
}
package com.example.smallchangebigloss;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
公共类 CurrentWeight 扩展 Activity {
private EditText edit;
private String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current_weight);
Bundle extras = getIntent().getExtras();
name = extras.getString("name");
edit = (EditText) findViewById(R.id.currentWeightEdit);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.current_weight, menu);
return true;
}
public void next(View view) {
if (edit.getText().toString().equals("")) {
new AlertDialog.Builder(this).setTitle("Ut Oh!")
.setMessage("Please enter your current weight.")
.setNeutralButton("Try again", null).show();
}
else {
Intent i = new Intent(getApplicationContext(),GoalWeight.class);
i.putExtra("name", name);
i.putExtra("current", edit.getText().toString());
startActivity(i);
}
}
}
package com.example.smallchangebigloss;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
公共类 GoalWeight 扩展 Activity {
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_goal_weight);
Bundle extras = getIntent().getExtras();
System.out.println(extras.containsKey("name"));
extras.containsKey("current");
edit = (EditText) findViewById(R.id.currentWeightEdit);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.goal_weight, menu);
return true;
}
public void next(View view) {
if (edit.getText().toString().equals("")) {
new AlertDialog.Builder(this).setTitle("Ut Oh!")
.setMessage("Please enter your Goal Weight.")
.setNeutralButton("Try again", null).show();
}
else {
Intent i = new Intent(getApplicationContext(), Complete.class);
i.putExtra("goal", edit.getText().toString());
startActivity(i);
}
}
}
package com.example.smallchangebigloss;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class Complete extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
// TextView current = (TextView) findViewById(R.id.completeCurrentMod);
// current.setText("hi");
//// current.setText(extras.getString("current")); These lines break it
setContentView(R.layout.activity_complete);
System.out.println("name: " + extras.getString("name") + "Current weight: "
+ extras.getString("current") + "goal: " + extras.getString("goal"));//Only displaying last ones...bundle them everytime in each class?
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.complete, menu);
return true;
}
}