码迷,mamicode.com
首页 > 其他好文 > 详细

intent参数的回传

时间:2014-09-27 03:02:29      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   ar   for   sp   div   art   

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参数的回传

标签:style   blog   color   os   ar   for   sp   div   art   

原文地址:http://www.cnblogs.com/ltflike/p/3995860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!