Android activity之间传递数据方式

public class MainActivity extends Activity {
	
	private Button btnStartAtv1;
	
	private TextView tvIn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btnStartAtv1=(Button) findViewById(R.id.btnStart);
		tvIn=(TextView) findViewById(R.id.tvIn);
		btnStartAtv1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//启动atv1
				Intent intent=new Intent(MainActivity.this,atv1.class);
//				intent.putExtra("txt", "Hello atv1");
				
				Bundle data=new Bundle();
				data.putString("txt", "Hello atv1");
				intent.putExtras(data);
				
//				startActivity(intent);
				startActivityForResult(intent, 0);
			}
		});
		
		
		//传回值时调用
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		tvIn.setText(data.getStringExtra("txt"));
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
}

以上是main activity

以下是从main activity 打开的activity

public class atv1 extends Activity {
	
	private Button btnClose;
	
	private TextView tvOut;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//与布局文件关联在一起了
		super.onCreate(savedInstanceState);
		setContentView(R.layout.atv1);
		
		btnClose=(Button) findViewById(R.id.btnClose);
		btnClose.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 返回值
				Intent intent=new Intent();
				intent.putExtra("txt", "Hello mainAct.");
				setResult(0, intent);
				
				
				//关闭activate
				finish();
			}
		});
		
		tvOut=(TextView) findViewById(R.id.tvOut);
		Bundle data=getIntent().getExtras();
		tvOut.setText(data.getString("txt"));
//		tvOut.setText(getIntent().getStringExtra("txt"));
		
	}
}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.l005.MainActivity" >

    <TextView
        android:id="@+id/tvIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="20dp"
        android:text="Start Atv1" />

</RelativeLayout>

atv1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnClose"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关闭" />

    <TextView
        android:id="@+id/tvOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值