第一个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>