隐式传递数据通常使用intent.setData(Uri.parse("ldw:canshu"))携带参数。
显示传递数据有2种携带数据的方法一种是intent,另一种是bundle携带参数。
intent方式:
intent方式携带参数:
//数据封装到intent中,前面是参数名后面是参数的值name-value的形式 intent.putExtra("name1", "A"); intent.putExtra("name2", "B");intent方式获取参数://从intent对象中取出封装好的数据 String name1 = intent.getStringExtra("name1"); String name2 = intent.getStringExtra("name2");bundle方式:bundle方式携带参数:
//把数据封装到bundle对象中,bundle是以keyvalue的形式传递数据 Bundle bundle = new Bundle(); bundle.putString("name1", "A"); bundle.putString("name2", "B"); //把bundle对象封装到intent对象中 intent.putExtras(bundle);bundle方式获取参数:Bundle bundle = intent.getExtras(); String name1 = bundle.getString("name1"); String name2 = bundle.getString("name2");显示跳转需要配置清单文件,这里面我们准备跳转到SecondActivity
<activity android:name="com.ldw.lifecycle.SecondActivity"/>第一个activity的代码如下:activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这个是测算" /> <EditText android:id="@+id/name1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="A" /> <EditText android:id="@+id/name2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="B" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转" android:onClick="click" /></LinearLayout>MainActivity.javapackage com.ldw.lifecycle;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity { @Override PRotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.out.println("第一个onCreate方法调用"); } public void click(View v){ Intent intent = new Intent(this,SecondActivity.class); //数据封装到intent中,前面是参数名后面是参数的值 intent.putExtra("name1", "A"); intent.putExtra("name2", "B"); startActivity(intent); } }第二个activity的代码如下:activity_second.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/res" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转到第一个activity" android:onClick="click1" /></LinearLayout>SecondActivity.javapackage com.ldw.lifecycle;import java.util.Random;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.TextView;public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent = getIntent(); //从intent对象中取出封装好的数据 String name1 = intent.getStringExtra("name1"); String name2 = intent.getStringExtra("name2"); Random rd = new Random(); int result = rd.nextInt(100); TextView res = (TextView) findViewById(R.id.res); res.setText(name1 + "和" + name2 + "的测试结果是" +result); } }使用bundle传递数据的时候,代码如下,布局文件不需要更改MainActivity.java
package com.ldw.lifecycle;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.out.println("第一个onCreate方法调用"); } public void click(View v){ Intent intent = new Intent(this,SecondActivity.class); //数据封装到intent中,前面是参数名后面是参数的值name-value的形式 //intent.putExtra("name1", "A"); //intent.putExtra("name2", "B"); //把数据封装到bundle对象中,bundle是以keyvalue的形式传递数据 Bundle bundle = new Bundle(); bundle.putString("name1", "A"); bundle.putString("name2", "B"); //把bundle对象封装到intent对象中 intent.putExtras(bundle); startActivity(intent); } }SecondActivity.javapackage com.ldw.lifecycle;import java.util.Random;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); Intent intent = getIntent(); //从intent对象中取出封装好的数据 //String name1 = intent.getStringExtra("name1"); //String name2 = intent.getStringExtra("name2"); Bundle bundle = intent.getExtras(); String name1 = bundle.getString("name1"); String name2 = bundle.getString("name2"); Random rd = new Random(); int result = rd.nextInt(100); TextView res = (TextView) findViewById(R.id.res); res.setText(name1 + "和" + name2 + "的测试结果是" +result); } }
新闻热点
疑难解答