天天看点

Android:不同Activity之间的数据传递

在Activity中调用另一个Activity时,需要调用startActivity(Intent i), 若需要在调用另外一个Activity的同时传递数据,那么就需要利用android.os.Bundle对象封装数据的能力,将欲传递的数据或参数,通过Bundle来传递不同Intent之间的数据。Bundle对象针对了不同的数据类型提供了许多的方法,例如,传递String类型的数据,使用的方法为Bundle.putString(stringName,stringValue):

而要传递Double类型的数据,使用的方法为Bundle.putDouble(doubleName,doubleValue),如下:bundle.putDouble("height", heightNum);

下面这一段代码是打包数据并传递:

Bundle bundle = new Bundle(); bundle.putString("name", nameStr); bundle.putDouble("height", heightNum); intent.putExtras(bundle); startActivity(intent);

在Activity2要如何接收来自Activity1传递来的数据呢?试想,在Activity1是以Bundle封装对象,自然在Activity2亦是以Bundle的方式解开封装的数据;程序中以getIntent().getExtras() 方法取得随着Bundle对象传递过来的数据。若要由Bundle对象中取出数据,则使用Bundle.getString(stringName)、Bundle.getDouble(doubleName) 等相对应的方法即可。

下面这段代码用来解析传递过来的数据:

Bundle bundle = this.getIntent().getExtras(); String name = bundle.getString("name"); Double height = bundle.getDouble("height");

最后,还是用一个小程序来演示一下:

首先是 activity 1:

package com.zx.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class BundleTest extends Activity {     EditText nameWidget, heightWidget;     Button btnSubmit;          @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);                  //get name and height         nameWidget = (EditText)findViewById(R.id.nameText);         heightWidget = (EditText)findViewById(R.id.heightText);         btnSubmit = (Button)findViewById(R.id.submit);         //btnSubmit.setOnClickListener(new Button.OnClickListener()         btnSubmit.setOnClickListener(new Button.OnClickListener()         {             @Override             public void onClick(View v) {                 // TODO Auto-generated method stub                 String nameStr = null;      Double heightNum = 0.0;            nameStr = nameWidget.getText().toString();      heightNum = Double.parseDouble(heightWidget.getText().toString());            Log.i("zx", "name is " + nameStr + " height=" + heightNum);      Intent intent = new Intent();      intent.setClass(BundleTest.this, ResultPage.class);            Bundle bundle = new Bundle();      bundle.putString("name", nameStr);      bundle.putDouble("height", heightNum);            intent.putExtras(bundle);      startActivity(intent);             }         });              } }

接下来是第二个activity的代码:

package com.zx.test; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class ResultPage extends Activity {     @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.result);                  Bundle bundle = this.getIntent().getExtras();         String name = bundle.getString("name");         Double height = bundle.getDouble("height");                  TextView tv = (TextView)findViewById(R.id.TextView01);         tv.setText("your name is " + name + ", your height is "+ height.toString());              } }