既然是activity之间的数据传递 肯定有两个activity 我们先创建两个activity,在MainActivity中
添加一个按钮组件 点击按钮 就跳转到其它的Activity 实现数据的传递
实现activity之间的跳转可以通过显示意图来实现,像这样
Intent intent=new Intent(); intent.setClass(MainActivity.this, OtherActivity.class); startActivity(intent);
intent.putExtra("姓名", "我是苏苏");
在OtherActivity里面通过getIntent().getExtra()来获得Intent对象里面的数据,getExtra()返回的是Bundle对象
通过Bundle对象接收,然后在日志文件中打印
Bundle bundle=getIntent().getExtras(); String name=bundle.getString("姓名"); Toast.makeText(OtherActivity.this, name, Toast.LENGTH_LONG).show();运行结果如下 可以发现我们把数据从一个Activity传递到了另外一个activity
当然也可以传递其它的基本数据类型 比如Double int等等 只要再接收的时候改变一下 比如接收double类型的
Double x=bundle.getDouble(key);
附上代码
MainActivity
package com.example.activity; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button=(Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(); intent.setClass(MainActivity.this, OtherActivity.class); intent.putExtra("姓名", "我是苏苏"); startActivity(intent); } }); } }
package com.example.activity; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class OtherActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_other); Bundle bundle=getIntent().getExtras(); String name=bundle.getString("姓名"); Toast.makeText(OtherActivity.this, name, Toast.LENGTH_LONG).show(); } }
<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=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="136dp" android:text="点我" /> </RelativeLayout>
<?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" > </LinearLayout>ActivityManifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activity" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.activity.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> <activity android:name="com.example.activity.OtherActivity" ></activity> </application> </manifest>