这是我想要做的:
我创建了类用户,其中包含以下内容:
public class User {
private String name;
private String gender;
private int age;
public User() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", gender=" + gender + ", age=" + age
+ "]";
}
}
在Activity UserIntro中,用户可以输入姓名,年龄和性别,我想在User中设置这些值,然后在其他Activity中显示。后者我也希望在SQLite中存储这些信息,但现在可以了。
UserInfoActivity 如下所示:
public class UserIntroActivity extends Activity {
public boolean started;
EditText userName;
ImageButton girl;
ImageButton boy;
EditText age;
String pol;
String name;
int userAge;
User user = new User();
public boolean isStarted() {
return started;
}
public void setStarted(boolean started) {
this.started = started;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_intro);
girl = (ImageButton) findViewById(R.id.imageButtonGirl);
boy = (ImageButton) findViewById(R.id.imageButtonBoy);
age = (EditText) findViewById(R.id.editTextAge);
Button btnGo = (Button) findViewById(R.id.buttonNext);
girl.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pol = "zensko";
user.setGender(pol);
}
});
boy.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pol = "musko";
}
});
btnGo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
userName = (EditText) findViewById(R.id.editTextName);
Intent intent = new Intent(UserIntroActivity.this,
EnterActivity.class);
startActivity(intent);
name = userName.getText().toString();
userAge = Integer.parseInt(age.getText().toString());
user.setName(name);
user.setAge(userAge);
Log.d("USER", name + " , " + userAge + " , " + pol);
finish();
}
});
}
}
在 EnterActivity 中,我会显示名称,a和性别。
public class EnterActivity extends Activity {
User user = new User();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter);
TextView test = (TextView) findViewById(R.id.textView1);
test.setText("ime: " + user.toString());
}
}
但是当我这样做时,结果是null,0,null所以我看到user.set ...()没有设置值。有谁知道为什么以及如何解决问题?谢谢大家!
答案 0 :(得分:5)
Google可以通过小搜索告诉您。
Intent intent= new Intent(this,EnterActivity .class);
intent.putExtra("txt", txtview.getText().toString());
startActivity(intent);
然后检索你的文字:
String receive = getActivity().getIntent().getExtras().getString("txt");