当前位置: 代码迷 >> Android >> android企图传参返回结果(六)
  详细解决方案

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

热度:6   发布时间:2016-04-28 02:00:48.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();				}			});




  相关解决方案