Activity中Intent传递数据--Bundle

<span style="font-size:18px;">///mainAvtivity//</span>
<span style="font-size:18px;">package com.demo.clf;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

	private EditText et_name1;
	private EditText et_name2;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		et_name1 = (EditText) findViewById(R.id.name1);
		et_name2 = (EditText) findViewById(R.id.name2);
	}
	
	public void click(View v){
		String name1 = et_name1.getText().toString();
		String name2 = et_name2.getText().toString();
		
		Intent intent = new Intent(this, SecondActivity.class);
//		intent.putExtra("name1", name1);
//		intent.putExtra("name2", name2);
		
		<span style="color:#ff0000;">//用bundle对象存储数据
		Bundle extras = new Bundle();
		extras.putString("name1", name1);
		extras.putString("name2", name2);
		intent.putExtras(extras);
		startActivity(intent);</span>
	}
	
}
</span>
<span style="font-size:18px;">/secondActivity/</span>
<pre name="code" class="java">public class SecondActivity extends Activity {
	
	private TextView tv_name1;
	private TextView tv_name2;
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		
		tv_name1 = (TextView) findViewById(R.id.name1);
		tv_name2 = (TextView) findViewById(R.id.name2);
		
		Intent intent = getIntent();
//		String name1 = intent.getStringExtra("name1");
//		String name2 = intent.getStringExtra("name2");
		
		//获取bundle对象
		Bundle bundle = intent.getExtras();
		String name1 = bundle.getString("name1");
		String name2 = bundle.getString("name2");
		
		tv_name1.setText(name1);
		tv_name2.setText(name2);
		
	}
}


 

<span style="font-size:18px;">//xml需要注册secondActivity///</span>
<span style="font-size:18px;"><activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
       <span style="color:#cc0000;"> <activity
            android:name=".SecondActivity"
            android:label="SecondActivity" >
            <intent-filter>
                <action android:name="com.meldoy.demo" />


                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter></span>
        </activity>
</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值