码迷,mamicode.com
首页 > 移动开发 > 详细

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

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

标签:android意图传参返回结果

例如有两个页面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意图传参返回结果(六)

标签:android意图传参返回结果

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

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