首页 > 移动平台 > 详细

android意图传参返回结果(六)

时间:2015-03-15 23:03:03      阅读:313      评论:0      收藏:0      [点我收藏+]

例如有两个页面a,b。a页面传参数到b页面,b页面将获得到的参数处理后,再将结果用参数传回来给页面a.

1.a页面的主要MainActivity类的代码如下:

    private Button button;
    private EditText result;
    private final static int REQUESTCODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        button = (Button)this.findViewById(R.id.button1);
        result = (EditText)this.findViewById(R.id.result);
        
        button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this,OtherActivity.class);
				intent.putExtra("a", "true");
				
				startActivityForResult(intent,REQUESTCODE);
				
			}
		});
        
    }
<pre name="code" class="html">    @Override
    protected void onActivityResult(int requestCode,int resultCode,Intent data){
    	super.onActivityResult(requestCode, resultCode, data);
    	if(resultCode == 2){
    		if(requestCode == REQUESTCODE){
                        <span style="font-family: Arial, Helvetica, sans-serif;">String c= intent.getStringExtra("c");</span>
    			result.setText(c);
    		}
    	}
    }
    

将参数a传入到b页面,onActivityResult函数处理将b页面传过来的参数c


<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">2.OtherActivity.java函数处理a参数后,将c参数传入回来给a页面. 在b页面的onCreate里面处理</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html">   <pre name="code" class="html">			@Override
			public void onClick(View arg0) {
					// TODO Auto-generated method stub
			        Intent intent = getIntent();
			        String a = intent.getStringExtra("a");
			        String c;
			        if(a=="true"){
			        	c = "It is True";
			        }else{
			        	c = "It is false";
			        }
			        
					Intent newIntent = new Intent();
					newIntent.putExtra("c", c);
			
					setResult(2,intent);
					finish();
				}
			});






android意图传参返回结果(六)

原文:http://blog.csdn.net/loveheronly/article/details/44281065

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!