页面一如下: | 页面二如下: |
逻辑描述在页面二
实现代码如下 :
1.创建一个User类:如下:
1. package com.ycp.intentforback; 2. 3. import java.io.Serializable; 4. 5. public class User implements Serializable { 6. String name; 7. String age; 8. 9. public User() { 10. 11. } 12. public User(String name, String age) { 13. this.name = name; 14. this.age = age; 15. } 16. 17. public String getName() { 18. return name; 19. } 20. 21. public String getAge() { 22. return age; 23. } 24. 25. public void setName(String name) { 26. this.name = name; 27. } 28. 29. public void setAge(String age) { 30. this.age = age; 31. } 32. }
这个User类主要作用是页面一传值和页面二接收值及回传值时对数据进行存储的中间层。
页面一逻辑代码如下:
1. protected void onCreate(Bundle savedInstanceState) { 2. super.onCreate(savedInstanceState); 3. setContentView(R.layout.activity_main); 4. et_name = (EditText) findViewById(R.id.et_name); 5. et_age = (EditText) findViewById(R.id.et_age); 6. login = (Button) findViewById(R.id.login); 7. 8. login.setOnClickListener(new View.OnClickListener() { 9. @Override 10. public void onClick(View v) { 11. Intent intent = new Intent(MainActivity.this,Main2Activity.class); 12. User user = new User(); 13. String s = et_name.getText().toString(); 14. String s1 = et_age.getText().toString(); 15. user.setName(s); 16. user.setAge(s1); 17. 18. intent.putExtra("goto",user); 19. startActivityForResult(intent,101); 20. 21. } 22. }); 23. 24. } 25. //接收回传值方法 26. @Override 27. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { 28. super.onActivityResult(requestCode, resultCode, data); 29. Userback userback = (Userback) data.getSerializableExtra("forback"); 30. et_name.setText(userback.getWork()); 31. et_age.setText(userback.getXingzuo()); 32. } 33. }
页面二代码如下·:
1. package com.ycp.intentforback; 2. 3. import android.content.Intent; 4. import android.support.v7.app.AppCompatActivity; 5. import android.os.Bundle; 6. import android.view.View; 7. import android.widget.Button; 8. import android.widget.EditText; 9. import android.widget.TextView; 10. 11. import java.io.Serializable; 12. 13. public class Main2Activity extends AppCompatActivity { 14. 15. private TextView tv_name; 16. private TextView tv_age; 17. private EditText et_work; 18. private EditText et_xingzuo; 19. private Button btn; 20. 21. @Override 22. protected void onCreate(Bundle savedInstanceState) { 23. super.onCreate(savedInstanceState); 24. setContentView(R.layout.activity_main2); 25. tv_name = (TextView) findViewById(R.id.tv_name); 26. tv_age = (TextView) findViewById(R.id.tv_age); 27. 28. et_work = (EditText) findViewById(R.id.et_work); 29. et_xingzuo = (EditText) findViewById(R.id.et_xingzuo); 30. btn = (Button) findViewById(R.id.btn); 31. 32. Intent intent = getIntent(); 33. User user =(User) intent.getSerializableExtra("goto"); 34. tv_name.setText(user.getName()); 35. tv_age.setText(user.getAge()); 36. 37. btn.setOnClickListener(new View.OnClickListener() { 38. @Override 39. public void onClick(View v) { 40. Intent intent1 = new Intent(); 41. String s = et_work.getText().toString(); 42. String s1 = et_xingzuo.getText().toString(); 43. Userback userback = new Userback(s,s1); 44. intent1.putExtra("forback",userback); 45. setResult(1,intent1); 46. finish(); 47. } 48. }); 49. } 50. }