逻辑描述在页面二
实现代码如下 :
1.创建一个User类:如下:
package com.ycp.intentforback;
import java.io.Serializable;
public class User implements Serializable {
String name;
String age;
public User() {
}
public User(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(String age) {
this.age = age;
}
}
这个User类主要作用是页面一传值和页面二接收值及回传值时对数据进行存储的中间层。
页面一逻辑代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = (EditText) findViewById(R.id.et_name);
et_age = (EditText) findViewById(R.id.et_age);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,Main2Activity.class);
User user = new User();
String s = et_name.getText().toString();
String s1 = et_age.getText().toString();
user.setName(s);
user.setAge(s1);
intent.putExtra("goto",user);
startActivityForResult(intent,101);
}
});
}
//接收回传值方法
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Userback userback = (Userback) data.getSerializableExtra("forback");
et_name.setText(userback.getWork());
et_age.setText(userback.getXingzuo());
}
}
页面二代码如下·:
package com.ycp.intentforback;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.Serializable;
public class Main2Activity extends AppCompatActivity {
private TextView tv_name;
private TextView tv_age;
private EditText et_work;
private EditText et_xingzuo;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv_name = (TextView) findViewById(R.id.tv_name);
tv_age = (TextView) findViewById(R.id.tv_age);
et_work = (EditText) findViewById(R.id.et_work);
et_xingzuo = (EditText) findViewById(R.id.et_xingzuo);
btn = (Button) findViewById(R.id.btn);
Intent intent = getIntent();
User user =(User) intent.getSerializableExtra("goto");
tv_name.setText(user.getName());
tv_age.setText(user.getAge());
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
String s = et_work.getText().toString();
String s1 = et_xingzuo.getText().toString();
Userback userback = new Userback(s,s1);
intent1.putExtra("forback",userback);
setResult(1,intent1);
finish();
}
});
}
}