Android: Set Text View's text from another act

2019-08-03 02:08发布

问题:

I have 2 activities and 2 classes.

In my Main class, when i click submit button it will start another activity. here is the code.

public void onClick(View v) {
    startActivity(new Intent(MainActivity.this, NewActivity.class));
    newActivity.setViewValues(fNameET.getText().toString(), lNameET.getText().toString(), mInitialET.getText().toString(), "Female", "birthday", addressET.getText().toString(), cNumberET.getText().toString());   
}

The newActivity is an object of the other activity and the setViewValues is the method of it.

This doesn't work, this is how i do it in java gui. Maybe something is missing.

Could anyone help me with this?

回答1:

You should pass the data like this.

MainActivity.java

Intent intent = new Intent(MainActivity.this, NewActivity.class));
intent.putExtra("firstName",fvalue);
intent.putExtra("lastname",lname);
......
startActivity(intent);

NewActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layoutname);
    Intent intent = getIntent();
    String firstName = intent.getStringExtra("firstName");
    String lastName = intent.getStringExtra("lastname");
}