首页 > 代码库 > intent参数的回传
intent参数的回传
1.
public class MainActivity extends Activity { private EditText editText1, editText2, editText3; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) this.findViewById(R.id.button1); editText1 = (EditText) this.findViewById(R.id.editText1); editText2 = (EditText) this.findViewById(R.id.editText2); editText3 = (EditText) this.findViewById(R.id.editText3); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, NextActivity.class); String a = editText1.getText().toString().trim(); String b = editText2.getText().toString().trim(); intent.putExtra("message", a + "+" + b + "=" + "?"); startActivityForResult(intent, 1000); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if(requestCode==1000&&resultCode==1001){ editText3.setText(data.getStringExtra("result")); } }
2.
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.next); button=(Button)this.findViewById(R.id.button1); textView=(TextView)this.findViewById(R.id.textView1); editText=(EditText)this.findViewById(R.id.editText1); Intent intent=getIntent(); String message=intent.getStringExtra("message"); textView.setText(message); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String result=editText.getText().toString().trim(); Intent intent=new Intent(); intent.putExtra("result", result); //参数回传 setResult(1001, intent); finish(); } }); }
说明:用startActivityForResult(intent, 1000);启动intent ,重写onActivityResult方法,在跳转的activity中用setResult(1001, intent);返回,并finish()结束
intent参数的回传
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。