1.Android中的Intent对象中包含了多个putXXX()方法(如putExtra()方法)用来插入不同类型的额外数据,也包含了多个getXXX()方法(如getStringExtra()、getIntExtra()方法)来读取数据,如下图Android API截图所示,其中只截了一小部分:
2.下面用一个小例子来讲述如何使用Intent传递数据。
(1).首先,新建一个Android项目,项目名为Intent_test,项目结构图如下所示:
(2).首先,打开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:orientation="vertical"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/button"/> </LinearLayout>
其中的文本我把它放在了values目录下的strings.xml文件,代码如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">简单的Intent传递数据</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="button">使用Intent传递数据</string> </resources>
然后我们再创建一个布局文件,即结构图中的activity_second.xml文件,布局为线性布局,加上一个TextView文本视图控件,代码如下:
<?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/textView" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
(3).接下来我们打开MainActivity.java文件,代码如下:
package com.android.intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private Button button;// 声明按钮控件 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);// 加载activity_main.xml这个布局文件 button = (Button) findViewById(R.id.button);// 通过资源Id的方式(即findViewById方法)获取到布局文件里的Button控件 // 添加监听器,即设置单击按钮后触发的事件 button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(MainActivity.this, SecondActivity.class);// 实例化Intent对象 /* 通过使用putExtra()方法设置值 */ intent.putExtra("name", "张三"); intent.putExtra("sex", "男"); intent.putExtra("age", 23); intent.putExtra("work", "学生"); startActivity(intent);// 将Intent传递给Activity,点击按钮后跳转到SecondActivity } }); } }
然后新建一个SecondActivity.java类,代码如下:
package com.android.intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class SecondActivity extends Activity { private TextView textView;// 声明TextView文本视图控件 @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_second);// 加载activity_second.xml布局文件 textView = (TextView) findViewById(R.id.textView);// 获取到该布局文件的textView控件 Intent intent = getIntent();// 获取到Intent,即从MainActivity传递过来的Intent String name = intent.getStringExtra("name");// 获取到MainActivity里putExtra()方法中的键为name的值,下面的类似 String sex = intent.getStringExtra("sex");// 获取传递过来的性别 int age = intent.getIntExtra("age", 0);// 获取传递过来的年龄 String work = intent.getStringExtra("work");// 获取传递过来的工作 textView.setText("名字为:" + name + "\n" + "性别为:" + sex + "\n" + "年龄为:" + age + "\n" + "工作为:" + work);// 设置文本,显示信息在activity中 } }
(4).因为我们新创建了一个Activity,所以必须在清单文件AndroidManifest.xml文件里配置SecondActivity这个activity,即加上这行代码即可:
<activity android:name=".SecondActivity" > </activity>
清单文件代码如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.intent" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.android.intent.MainActivity" 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=".SecondActivity" > </activity> </application> </manifest>
(5).接着我们把此项目部署到安卓模拟器上,效果如下:
点击按钮,如下图所示:
(6).如果我在第五小步中没有在清单文件里添加SecondActivity这个activity的话,再点击使用Intent传递数据按钮的话,模拟器就会弹出下图的对话框:
而在LogCat这个视图下,报错情况,如下图所示:
上图蓝色框框起来的即是为什么会报错的原因,android.content.ActivityNotFoundException异常,即Activity找不到异常,详细说明,不能寻找到activity类,这个类位于com.android.intent包下的SecondActivity,你有没有在AndroidManifest.xml文件中声明这个activity?
这个异常信息解读就是这样,所以我们必须在AndroidManifest.xml文件中声明com.android.intent包下的SecondActivity这个Activity,即上面第五小步。所以以后报这种错误,记得检查,新建了一个Activity类,必须在AndroidManifest.xml文件中声明,新手经常忘记这一步的......
3.以上就是使用Intent传递数据的全部内容,仅供大家学习参考,写得不好,请见谅,如有错误,请指出,谢谢!