main端:
/** * 实现:主页面点击按钮1,跳转到页面二输入内容,点击页面二按钮返回主页面并显示输入信息; * 主页面点击按钮2,跳转到页面三输入内容,点击页面三按钮返回主页面并显示输入信息. * 主要步骤: * 1) startActivityForResult(intent, 0); * 2) 回传数据时,调用setResult(0, data)方法,然后执行finish() * 3) onActivityResult(int, int, Intent data) */ public class MainActivity extends Activity { private static final int BTN_ONE = 1;//将常量封装定义 private static final int BTN_TWO = 2; @Override//重写onActivityResult protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);//有参数的super必须重写 TextView textView = (TextView) findViewById(R.id.textView1_main); if(requestCode == BTN_ONE)//根据请求码判断接收内容 { String str = data.getStringExtra("one"); textView.setText(str); } else if(requestCode == BTN_TWO) { String str = data.getStringExtra("two"); textView.setText(str); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //监听button1,跳转到NextActivity,等待result Button button1=(Button) findViewById(R.id.button1_main); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,NextActivity.class); startActivityForResult(intent, BTN_ONE); } }); //监听button2,跳转到ThirdActivity,等待result Button button2 =(Button) findViewById(R.id.button2_main); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,ThirdActivity.class); startActivityForResult(intent, BTN_TWO); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
next端:
public class NextActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); Button button =(Button) findViewById(R.id.button1_next); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //得到用户数据 EditText editText=(EditText) findViewById(R.id.editText1_next); String strone = editText.getText().toString(); //回传数据 Intent data = new Intent(); data.putExtra("one", strone); setResult(1, data ); //关闭当前窗口 finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.next, menu); return true; } }
third端:
public class ThirdActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_third); Button button = (Button) findViewById(R.id.button1_third); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { EditText editText = (EditText) findViewById(R.id.editText1_third); String strtwo = editText.getText().toString(); Intent data = new Intent(); data.putExtra("two", strtwo); setResult(2,data ); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.third, menu); return true; } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。