首页 > 代码库 > 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参数的回传