雾山的Android学习笔记---Intent
2014-05-14 20:43
337 查看
在android平台中,Activity组件之间的交互通过意向(Intent)来实现。
Intent是android的组件之一(激活组件,用于激活其他组件),Intent对象在android系统中代表一种意图,Intent中最重要的内容是action与data
使用Intent对象传递数据的步骤:
1)在Activity之间可以使用Intent对象传递数据
2)使用putExtra()系列方法向Intent对象当中储存数据
3)使用getXXXExtra()系列方法从Intent对象当中取出数据
下面是代码示例:
主Activity:
第二个Activity:
主Activity的XML代码:
副Activity的XML代码:
最后的结果图:
Intent是android的组件之一(激活组件,用于激活其他组件),Intent对象在android系统中代表一种意图,Intent中最重要的内容是action与data
使用Intent对象传递数据的步骤:
1)在Activity之间可以使用Intent对象传递数据
2)使用putExtra()系列方法向Intent对象当中储存数据
3)使用getXXXExtra()系列方法从Intent对象当中取出数据
下面是代码示例:
主Activity:
package com.tangbc.s02e04_intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; 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 arg0) { //生成一个Intent对象 Intent intent = new Intent(); //调用Intent对象的setClass()方法 //setClass(Context packageContext, Class<?> cls) intent.setClass(MainActivity.this, Other.class); //在该Intent对象中存入数据 intent.putExtra("com.tangbc.s02e04_intent.Age", 20); intent.putExtra("com.tangbc.s02e04_intent.Name","tang"); //启动第二个Activity startActivity(intent); } } }
第二个Activity:
package com.tangbc.s02e04_intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class Other extends Activity{ private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.other); //将返回启动这个Activity的Intent对象赋给intent Intent intent = getIntent(); //将intent中的数据取出并赋值 //括号中第二个数是默认值,若没有对应的值,就取默认值 int age = intent.getIntExtra("com.tangbc.s02e04_intent.Age", 10); int age1 = intent.getIntExtra("com.tangbc.s02e04_intent.Age1", 10); String name = intent.getStringExtra("com.tangbc.s02e04_intent.Name"); textView = (TextView)findViewById(R.id.textView); textView.setText("name= " + name + ",age= " + age + ",age1=" + age1); } }
主Activity的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" 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" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="touch me" /> </RelativeLayout>
副Activity的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="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
最后的结果图:
相关文章推荐
- 雾山的Android学习笔记---Intent
- Android学习笔记九之Intent
- android学习笔记(一)activity的基本用法和创建,bundle如何传递数据,intent的用法
- Android学习笔记-学习Intent的使用
- Android学习笔记02——Intent的使用
- 雾山的Android学习笔记---消息机制(三) .
- Android学习笔记-Activity&Intent
- Android学习笔记之Intent(1)
- android学习笔记3 Intent和Activity
- Android学习笔记(4)-学习Intent的使用
- [Android学习笔记] -- Activity和Intent
- Pro Android学习笔记(十):了解Intent(上)
- Android学习笔记之Intent
- Android学习笔记_46_Android的intent之间Object、List、List<Object>和全局变量数据的传递(Parcelable Serializable)
- Android学习笔记-学习Intent的使用
- 【android学习笔记】理解android.intent.action.MAIN 与 android.intent.category.LAUNCHER
- 【Android学习笔记】Intent详解
- android学习笔记——Intent
- 雾山的Android学习笔记---消息机制(二) .
- [转载] Pro Android学习笔记(十一):了解Intent(中)