Android:不同Activity之间的数据传递 (简单的demo)
来源:互联网 发布:网络女直播 编辑:程序博客网 时间:2024/05/16 09:58
在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;
/** Called when the activity is first created. */
@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());
}
}
- Android:不同Activity之间的数据传递 (简单的demo)
- Android:不同Activity之间的数据传递
- Android:不同Activity之间的数据传递
- Android之不同Activity之间的数据传递
- Android之不同Activity之间的数据传递
- 关于android不同activity之间数据传递的资料
- Android开发笔记---不同activity之间的数据传递
- Android之在不同的Activity之间传递数据
- Android中不同Activity之间的数据传递
- 不同Activity之间的数据传递
- 不同Activity之间的数据传递
- 不同Activity之间的数据传递
- 不同Activity之间的数据传递
- 在不同的Activity之间传递数据
- Activity之间数据的简单传递
- 【数据】不同Activity之间的数据传递——Bundle
- Android之不同Activity之间的数据传递(适用于少量长期不更改的数据)
- android不同进程之间的数据传递
- Android使用BitmapRegionDecoder获取指定区域的图片
- osg::BillBoard的用法
- 【BZOJ 1618】 [Usaco2008 Nov]Buying Hay 购买干草
- ZooKeeper源码分析:Quorum请求的整个流程
- JavaScript 中的"类"与"实例"
- Android:不同Activity之间的数据传递 (简单的demo)
- select监听多个socket的例子
- 互联网分享希望
- Java 动态代理
- Codeforces Round #285 (Div. 2) C. Misha and Forest
- iOS常用尺寸大小说明
- ssh自动登录工具
- MFC List Control滚动条
- linux编程|fork函数讲解