Intent和Bundle传值

第一个Activity
package com.example.yztc_activity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

/**
* intent/bundle传值
*/

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

// intent传值
public void send(View v) {
    Intent intent = new Intent(MainActivity.this, OtherActivity.class);
    intent.putExtra("name", "zhangsan");
    intent.putExtra("age", 20);
    intent.putExtra("score", 98.5);
    intent.putExtra("sex", '男');// 数组为单引号
    intent.putExtra("bl", true);
    startActivity(intent);
}

// bundle传值
public void bundle(View v) {
    // 1.创建intent意图对象
    Intent intent2 = new Intent(MainActivity.this, OtherActivity.class);
    // 2.创建bundle对象,存储需要传递的数据
    Bundle bundle = new Bundle();
    // 3.将需要传递的数据存储到bundle对象中
    bundle.putString("name", "lisi");
    bundle.putInt("age", 30);
    bundle.putDouble("score", 80.5);
    bundle.putChar("sex", '女');
    // 4.将bundle对象存储到intent对象中
    intent2.putExtras(bundle);
    // 5.启动Activity
    startActivity(intent2);
    }
}

<LinearLayout 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:orientation="vertical" >

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="send"
    android:text="intent传值" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="bundle"
    android:text="bundle传值" />

</LinearLayout>

第二个Activity
package com.example.yztc_activity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class OtherActivity extends Activity {

private TextView tv_show, tv_showinfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_other);

    tv_show = (TextView) findViewById(R.id.tv_show);
    tv_showinfo = (TextView) findViewById(R.id.tv_showinfo);

    // 1.获取激活组件的intent的对象
    Intent intent = getIntent();
    // 2.根据key获取传递的数据
    String name = intent.getStringExtra("name");
    int age = intent.getIntExtra("age", 0);// 0为默认值
    double score = intent.getDoubleExtra("score", 0.0);
    char sex = intent.getCharExtra("sex", ' ');
    boolean bl = intent.getBooleanExtra("bl", false);
    // 3.将数据展示到TextView控件中
    tv_show.setText("name:" + name + "\nage:" + age + "\nscore:" + score
            + "\nsex:" + sex + "\nbl:" + bl);

    // 获取激活组件的intent对象
    Intent intent2 = getIntent();
    // 获取传递的bundle对象
    Bundle bundle = intent.getExtras();
    // 在bundle根据key获取具体数据
    String name2 = bundle.getString("name");
    int age2 = bundle.getInt("age");
    double score2 = bundle.getDouble("score");
    char sex2 = bundle.getChar("sex");
    tv_show.setText("name:" + name2 + "\nage:" + age2 + "\nscore:" + score2
            + "\nsex:" + sex2);
    }

}

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

<TextView
    android:id="@+id/tv_show"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#ff0000"
    android:textSize="25sp" />

<TextView
    android:id="@+id/tv_showinfo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#00ff00"
    android:textSize="25sp" />

</RelativeLayout>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值