• 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>




  • 相关阅读:
    JavaScript -- BATweb笔试面试
    1--html属性
    0--node安装
    1-- express
    lsof命令
    1--字符串和数组的指向问题
    19--复杂链表的复制。
    18--二叉树中和为某一值的路径
    剑指offer——64和为s的数字
    剑指offer——04二维数组中的查找
  • 原文地址:https://www.cnblogs.com/flyingsir/p/2263746.html
Copyright © 2020-2023  润新知