利用Intent进行Activity之间的通信(1)
2014-05-07 09:48
405 查看
Intent对象的基本概念:它是Android应用程序组件之一,表示一种意图。Intent当中最重要的内容是action与data.
使用Intent对象传递数据的基本步骤为:
1、在需要传递数据的Activity中使用putExtra()系列方法向Intent对象当中存储数据;
2、在接受数据的Activity中使用getXXXExtra()系列方法从Intent对象当中取出数据。
下面用例子说明。
在第一个Activity中有一个按钮,点击后启动OtherActivity,并传递Intent对象,再把当中数据显示出来。
MainActivity.java
OtherActivity.java
activity_main.xml
other.xml
AndroidManifest.xml
使用Intent对象传递数据的基本步骤为:
1、在需要传递数据的Activity中使用putExtra()系列方法向Intent对象当中存储数据;
2、在接受数据的Activity中使用getXXXExtra()系列方法从Intent对象当中取出数据。
下面用例子说明。
在第一个Activity中有一个按钮,点击后启动OtherActivity,并传递Intent对象,再把当中数据显示出来。
MainActivity.java
package com.wyb.s02_e03_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); button =(Button)findViewById(R.id.button); button.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(MainActivity.this, OtherActivity.class); intent.putExtra("com.wyb.s02_e03_intent.Age", 20);//注意用双引号把报名括起来 intent.putExtra("com.wyb.s02_e03_intent.Name", "ZhangSan"); startActivity(intent); } } }
OtherActivity.java
package com.wyb.s02_e03_intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class OtherActivity extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); textView = (TextView)findViewById(R.id.textView); Intent intent = getIntent(); int age = intent.getIntExtra("com.wyb.s02_e03_intent.Age", 10); String name = intent.getStringExtra("com.wyb.s02_e03_intent.Name"); textView.setText("姓名:"+name+" 年龄:"+age+" "); } }
activity_main.xml
<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" tools:context="${packageName}.${activityClass}" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="启动第二个Activity" /> </RelativeLayout>
other.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/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="other"/> </LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wyb.s02_e03_intent" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.wyb.s02_e03_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="com.wyb.s02_e03_intent.OtherActivity" android:label="otherActivity"></activity> </application> </manifest>
相关文章推荐
- 利用Intent进行Activity之间的通信(2)
- Activity之间利用Intent进行数据的传递
- 使用Intent和Bundle在两个Activity之间进行通信添加上一步按钮实现回退
- ------------------------------利用BroadcastReceiver进行Activity和Service之间的通信(留着自己参考用)
- 利用Intent使Activity和Service通过BroadcastReceiver进行数据交互
- Activity与Activity利用接口进行数据通信
- Activity之间的通信(Intent)
- Android Intent 程序之间进行通信 传输数据
- android中多个activity之间不能实现Intent通信
- 利用Intent实现多个Activity之间的跳转
- android studio for android learning (十一) 利用bundle在activity之间进行数据传递示例
- 利用广播与Service之间进行通信
- 在Activity和Service之间使用Binder和回调接口进行通信
- [置顶] Android开发值利用Intent进行put传值,setclass启动activity,并用get进行取值
- Linux初学,利用管道文件进行通信,实现3个人之间的群聊
- Fragment和Activity之间进行通信
- Intent 进行activity 与activity之间的传值
- android中activity之间使用intent通信
- 进程间通信---在父进程跟子进程之间利用管道进行通信。一个简单的例子
- Fragment向Activity之间进行通信