Intent是Android应用程序组件之一,在Android系统当中表示一种意图,Intent中包含了一组信息:

  最重要的内容是action(动作)和data(数据)

  Component name 表示要启动哪个Activity

FirstActivity.java

 import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class FirstActivity extends Activity {
private Button firstButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first); firstButton = (Button)findViewById(R.id.firstButton);
firstButton.setText(R.string.firstButton);
firstButton.setOnClickListener(new ButtonListener());
} class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
Intent intent = new Intent();
/*setClass方法:
第一个参数是一个Context对像,Context是一个类,Activity是Context类的子类,也就是说,所有的Activity对象都可以向上转型为Context对象
第二个参数是一个Class对象,在当前场景下,传入需要启动的Activity类的Class对象
*/
intent.setClass(FirstActivity.this, SecondActivity.class);
FirstActivity.this.startActivity(intent);
} } }

FirstActivity.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=".FirstActivity"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/first" /> <Button
android:id="@+id/firstButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

SecondActivity.java

 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 SecondActivity extends Activity{
private Button SecondtButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
SecondtButton = (Button)findViewById(R.id.SecondtButton);
SecondtButton.setText(R.string.secondtButton);
SecondtButton.setOnClickListener(new ButtonListener()); }
class ButtonListener implements OnClickListener{ @Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(SecondActivity.this, ThirdActivity.class);
SecondActivity.this.startActivity(intent);
finish();//该方法使Activity跳转到下一个Activity时关掉这个Activity
} }
}

SecondActivity.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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/second"
></TextView> <Button
android:id="@+id/SecondtButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></Button>
</LinearLayout>

ThirdActivity.java

 import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class ThirdActivity extends Activity{
private Button ThirdButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third); ThirdButton = (Button)findViewById(R.id.ThirdActivity);
ThirdButton.setText(R.string.thirdtButton);
ThirdButton.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener{
public void onClick(View v) { Uri uri = Uri.parse("smsto://0800000123");
Intent it = new Intent(Intent.ACTION_SENDTO, uri);
it.putExtra("sms_body", "The SMS text");
ThirdActivity.this.startActivity(it);
}
}
}

ThirdActivity.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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/third"
/> <Button
android:id="@+id/ThirdActivity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/> </LinearLayout>

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mars.activity06"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mars.activity06.FirstActivity"
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的时候就需要使用<activity/>标签对该Activity进行注册 有两个属性name和lable name的值是这个Activity的包名和类名 lable可以自定义-->
<activity android:name="com.mars.activity06.SecondActivity" android:label="@string/second"/>
<activity android:name="com.mars.activity06.ThirdActivity" android:label="@string/third"/>"
</application> </manifest>

string.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">Activity06</string>
<string name="action_settings">Settings</string>
<string name="first">FirstActivity</string>
<string name="firstButton">FirstButton</string>
<string name="second">SecondActivity</string>
<string name="secondtButton">SecondButton</string>
<string name="third">ThirdActivity</string>
<string name="thirdtButton">ThirdButton</string> </resources>

在Activity之间可以通过Intent对象传递数据

  使用putExtra()系列方法向Intent对象当中存储数据

  使用getXXXExtra()系列方法从Intent对象当中取出数据

MainActivity.java

 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 myButton;
@Override
//复写父类当中的onCreate方法,Activity第一次运行时会调用这个方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new myButtonListener()); }
//以下是一个内部类,这个内部类的对象是一个监听器(如果对监听器不是很熟悉,可以参考设计模式当中的观察者模式)
class myButtonListener implements OnClickListener{ //生成该类的对象,并将其注册到控件上。如果该控件被用户按下,就会执行onClick方法
public void onClick(View v) { Intent intent = new Intent();//生成一个Intent对象 intent.putExtra("number", "13112266075");//在Intent对象当中添加一个键值对,键的取名应该加上这个Activity的包名代表着那个包下的数据,格式包名.键的名字,如果不需要传递数据,这步可以不要 //设置Intent对象要启动的Activity,不能直接写this,因为直接写this代表的是本类的对象,也就是myButtonListener这个对象
/*
* setClass函数的第一个参数是一个Context对象
* Context是一个类,Activity是Context类的子类,也就是说,所有的Activity对象,都可以向上转型为Context对象
* */
intent.setClass(MainActivity.this, OtherActivity.class);
//通过Intent对象启动另外一个Activity
MainActivity.this.startActivity(intent); /*
//以下的4行代码将启动发送短信的Activity,
Uri uri = Uri.parse("smsto://0800000123");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "The SMS text");
startActivity(intent);
*/
}
}
}

main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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" > <!-- 下面的标签声明了一个Buttin(按钮)控件,并为这个控件设置了ID,这个ID会被注册到R.java文件当中 -->
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Button"
/> </LinearLayout>

OtherActivity.java

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; public class OtherActivity extends Activity{
private TextView myTextView;
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.other);
//取得从上一个Activity当中传递过来的Intent对象
Intent intent = getIntent();
//从Intent当中根据key取得value
String number = intent.getStringExtra("number");//该方法还有第二个参数,是一个值,表示如果上一个Activity中的键没有值,就将这个方法中的第二个参数赋值number这个变量 myTextView = (TextView) findViewById(R.id.myTextView);
myTextView.setText(number); } }

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/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/TextView"
/>" </LinearLayout>

AndriodManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mars.conver_activity"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mars.conver_activity.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.mars.conver_activity.OtherActivity" android:label="@string/OtherActivity"/>"
</application> </manifest>

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">Activity转换</string>
<string name="action_settings">Settings</string>
<string name="Button">start next Button</string>
<string name="TextView">I am coming</string>
<string name="OtherActivity">OtherActivity</string> </resources>

MainActivity.java

 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;
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{
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("com.mars.second_intent.Age", 20);
intent.putExtra("com.mars.second_intent.Number", "13112266075");
intent.setClass(MainActivity.this, OtherActivity.class);
MainActivity.this.startActivity(intent);
}
}
}

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"
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" > <TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview1"
android:text="启动第二个Activity"
/> </RelativeLayout>

OtherActivity.java

 import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView; public class OtherActivity extends Activity{
private TextView textview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
Intent intent = getIntent();
String str = intent.getStringExtra("com.mars.second_intent.Number");
int age = intent.getIntExtra("com.mars.second_intent.Age", 10); textview = (TextView) findViewById(R.id.textview);
textview.setText("Number="+str+"age="+age);
}
}

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="wrap_content"
android:layout_height="wrap_content"
android:text="zhognguo"
/> </LinearLayout>

AndriodManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mars.second_intent"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="18" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mars.second_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.mars.second_intent.OtherActivity"
android:label="第二个Activity"
></activity>
</application> </manifest>

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">Second_Intent</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string> </resources>

最新文章

  1. liunx 磁盘管理命令记录
  2. htm常用标签总结
  3. hibernate save,update,saveorupdate方法有什么区别
  4. bootstrap学习笔记之三(组件的使用)
  5. 机器学习 —— 概率图模型(Homework: Exact Inference)
  6. STL总结之functor
  7. c#使用UIA进行模拟点击操作
  8. Servlet编码和解码
  9. ActiveMQ消息队列介绍(转)
  10. Asp.net mvc 知多少(五)
  11. Mybatis入门1
  12. sql注入中关于--+的一点探索
  13. MySQL&#160;慢查询日志配置与简析
  14. 今天开始学习模式识别与机器学习Pattern Recognition and Machine Learning (PRML),章节5.1,Neural Networks神经网络-前向网络。
  15. 自学Linux Shell16.1-函数概念
  16. intest
  17. day_07_python_1124
  18. 距离为K的节点 All Nodes Distance K in Binary Tree
  19. windows 网管常用命令
  20. Java之集合(五)LinkedList

热门文章

  1. FFmpeg封装格式处理4-转封装例程
  2. 在C#中,如何连接已加密的Sqlite数据库
  3. angularjs学习第九天笔记(指令作用域【隔离作用域】研究)
  4. LeetCode算法笔记目录
  5. anaconda更新库命令
  6. JAVA中的集合容器操作类
  7. 【原】通过BeanNameAutoProxyCreator改变臃肿代码
  8. 精选20道Java代码笔试题
  9. Tomcat意外宕机分析
  10. cookie、localStorage、sessionStorage的区别