android中activity的相互传值

MainActivity代码

package com.myactivitytest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private TextView textView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		textView = new TextView(this);
		textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		
		Button button = new Button(this);
		button.setText("设置金额");
		button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
		button.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				Intent localIntent = new Intent();
				localIntent.setClass(MainActivity.this, OrderActivity.class);
				startActivityForResult(localIntent,8888);
			}
		});
		
		LinearLayout linearLayout = new LinearLayout(this);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
		linearLayout.addView(textView);
		linearLayout.addView(button);
		
		setContentView(linearLayout);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent){
		System.out.println(requestCode);
		if(requestCode==8888&&resultCode==RESULT_OK&&intent.hasExtra("orderMoney")){
			textView.setText(intent.getExtras().getString("orderMoney"));
		}
	}

}

OrderActivity代码

package com.myactivitytest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class OrderActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		final EditText editText = new EditText(this);
		editText.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
		
		Button button = new Button(this);
		button.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
		button.setText("返回主界面");
		button.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				System.out.println("input is:"+editText.getText().toString());
				Intent localIntent = new Intent(); 
				localIntent.putExtra("orderMoney", editText.getText().toString());
				setResult(RESULT_OK,localIntent);
				finish();
			}
		});
		
		LinearLayout linearLayout = new LinearLayout(this);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
		linearLayout.addView(editText);
		linearLayout.addView(button);
		
		setContentView(linearLayout);
	}
}

猜你喜欢

转载自yunix.iteye.com/blog/2001566
今日推荐