Activity中Intent传递数据--Bundle

来源:互联网 发布:access数据库备注类型 编辑:程序博客网 时间:2024/05/16 13:41
<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 stubsuper.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
原创粉丝点击