Bundle简单传参示例
mainActivity
package com.gongsi.testbundle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class TestBundleActivity extends Activity {
private Button btn;
private View.OnClickListener c1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//找到button按钮
btn = (Button)findViewById(R.id.btn);
//设置按钮监听
c1 = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(TestBundleActivity.this, Target.class);
//建立Bundle并put进入不同类型键值对
Bundle bundle = new Bundle();
bundle.putString("Data", "ray'blog");
bundle.putDouble("double", 1.0);
intent.putExtras(bundle);
//启动
startActivity(intent);
finish();
}
};
btn.setOnClickListener(c1);
}
}
TargetActivity 接收到的bundle的值取出并log打印出来
package com.gongsi.testbundle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Target extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bundle bundle = getIntent().getExtras();
String data = bundle.getString("Data");
double dou = bundle.getDouble("double");
Log.v("info:","data:"+data+" "+"dou:"+dou);
}
}
main.xml简单布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/btn"
android:text="intent.putExtra(bundle)"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
mainfest主要是增加target Activity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gongsi.testbundle"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".TestBundleActivity"
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"/>
</application>
</manifest>