Android中Bundle使用
来源:互联网 发布:装修平面设计软件 编辑:程序博客网 时间:2024/06/06 17:26
每次创建一个Activity是都会有一个Bundle,但是对其的用法不清楚,今天就查了查并做了一个例子。
Android Developer上对Bundle的解释是:A mapping from String values to various Parcelable types.
就是说Bundle是一个键值对,就跟Java中的Map差不多。
使用Bundle并通过Intent可以让两个Activity之间传值。
实现步骤如下:
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.btn);
}
public void btnListener(View view){
Intent intent=new Intent();
intent.setClass(MainActivity.this, Target.class);
Bundle bd=new Bundle();
bd.putString("1","Bundle Test");
intent.putExtras(bd);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
MainActivity的布局文件activity_main.xml:
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:onClick="btnListener"
android:text="turn to next activity"
/>
</RelativeLayout>
第二个Activity是Target.java:
public class Target extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.target_layout);
TextView text=(TextView) findViewById(R.id.text);
Bundle b=getIntent().getExtras();
text.setText(b.getString("1"));
}
}
第二个Activity的布局文件target_layout.xml:
<?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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
/>
</LinearLayout>
AndroidMainfesi.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bundletest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.bundletest.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=".Target"></activity>
</application>
</manifest>
- Android中Bundle使用
- Android中Bundle使用
- Android中Bundle的使用
- android中bundle的使用
- Android中Bundle的使用示例
- Android中使用Bundle交换数据
- Android中使用Bundle页面传值
- Android中Bundle
- android 中bundle对象
- android中intent bundle
- Android Bundle 使用
- android bundle 使用注意
- android Bundle的使用
- Android Bundle类使用
- Intent中使用Bundle
- 在Android中通过Intent使用Bundle传递对象
- 在Android中通过Intent使用Bundle传递对象
- 在Android中通过Intent使用Bundle传递对象
- 编译FFMPEG Android版本
- [译]Kinect for Windows SDK开发入门(一):开发环境配置
- 微信公众帐号开发教程第11篇-符号表情的发送(上)
- php获取数组最后一个数组值的三个方法
- 文本文件和二进制文件的区别
- Android中Bundle使用
- 微信公众帐号开发教程第12篇-符号表情的发送(下)
- 可有一天的飞鸽传书
- 备忘录 asp.net mvc4 + ef5.0 + mysql 找不到或无法加载已注册的 .Net Framework Data Provider。
- UltraEdit使用
- LINUX小记
- printf 十六进制格式化输出 2字节对齐
- JSON 中JsonConfig的使用
- [有图有真相]Eclipse svn账户注销清除