zoukankan      html  css  js  c++  java
  • Android_2.2_eclips_Bundle简单传参demo

    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>




  • 相关阅读:
    第四十一篇-android studio 关闭自动保存功能
    《深入理解mybatis原理》 Mybatis初始化机制详解
    Java多线程系列--“基础篇”10之 线程优先级和守护线程
    Java多线程系列--“基础篇”09之 interrupt()和线程终止方式
    Java多线程系列--“基础篇”08之 join()
    Java多线程系列--“基础篇”07之 线程休眠
    Java多线程系列--“基础篇”06之 线程让步
    Java多线程系列--“基础篇”05之 线程等待与唤醒
    【深入Java虚拟机】之八:Java垃圾收集机制
    【深入Java虚拟机】之七:Javac编译与JIT编译
  • 原文地址:https://www.cnblogs.com/flyingsir/p/2263746.html
Copyright © 2011-2022 走看看