首页 > 其他 > 详细

多个Activity和Intent

时间:2014-03-07 21:08:20      阅读:405      评论:0      收藏:0      [点我收藏+]

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

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

  Component name 表示要启动哪个Activity

 

FirstActivity.java

bubuko.com,布布扣
 1 import android.os.Bundle;
 2 import android.app.Activity;
 3 import android.content.Intent;
 4 import android.view.View;
 5 import android.view.View.OnClickListener;
 6 import android.widget.Button;
 7 
 8 public class FirstActivity extends Activity {
 9     private Button firstButton;
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_first);
14         
15         firstButton = (Button)findViewById(R.id.firstButton);
16         firstButton.setText(R.string.firstButton);
17         firstButton.setOnClickListener(new ButtonListener());
18     }
19     
20     class ButtonListener implements OnClickListener{
21 
22         @Override
23         public void onClick(View v) {
24             Intent intent = new Intent();
25             /*setClass方法:
26                     第一个参数是一个Context对像,Context是一个类,Activity是Context类的子类,也就是说,所有的Activity对象都可以向上转型为Context对象
27                     第二个参数是一个Class对象,在当前场景下,传入需要启动的Activity类的Class对象
28             */
29             intent.setClass(FirstActivity.this, SecondActivity.class);
30             FirstActivity.this.startActivity(intent);
31         }
32         
33     }
34 
35 }
bubuko.com,布布扣

 FirstActivity.xml

bubuko.com,布布扣
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".FirstActivity" 
10     android:orientation="vertical">
11 
12     <TextView
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/first" />
16     
17     <Button
18         android:id="@+id/firstButton"
19         android:layout_width="fill_parent"
20         android:layout_height="wrap_content"/>
21 </LinearLayout>
bubuko.com,布布扣

 

SecondActivity.java

bubuko.com,布布扣
 1 import android.app.Activity;
 2 import android.content.Intent;
 3 import android.os.Bundle;
 4 import android.view.View;
 5 import android.view.View.OnClickListener;
 6 import android.widget.Button;
 7 
 8 public class SecondActivity extends Activity{
 9     private Button SecondtButton;
10     @Override
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_second);
14         SecondtButton = (Button)findViewById(R.id.SecondtButton);
15         SecondtButton.setText(R.string.secondtButton);
16         SecondtButton.setOnClickListener(new ButtonListener());
17         
18     }
19     class ButtonListener implements OnClickListener{
20 
21         @Override
22         public void onClick(View v) {
23             Intent intent = new Intent();
24             intent.setClass(SecondActivity.this, ThirdActivity.class);
25             SecondActivity.this.startActivity(intent);
26             finish();//该方法使Activity跳转到下一个Activity时关掉这个Activity
27         }
28         
29     }
30 }
bubuko.com,布布扣

 SecondActivity.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     
 7     
 8     <TextView
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:text="@string/second"
12         ></TextView>
13     
14     
15     <Button
16         android:id="@+id/SecondtButton"
17         android:layout_width="fill_parent"
18         android:layout_height="wrap_content"
19         ></Button>
20 </LinearLayout>
bubuko.com,布布扣

 

ThirdActivity.java

bubuko.com,布布扣
 1 import android.app.Activity;
 2 import android.content.Intent;
 3 import android.net.Uri;
 4 import android.os.Bundle;
 5 import android.view.View;
 6 import android.view.View.OnClickListener;
 7 import android.widget.Button;
 8 
 9 public class ThirdActivity extends Activity{
10     private Button ThirdButton;
11     protected void onCreate(Bundle savedInstanceState) {
12         super.onCreate(savedInstanceState);
13         setContentView(R.layout.activity_third);
14         
15         ThirdButton = (Button)findViewById(R.id.ThirdActivity);
16         ThirdButton.setText(R.string.thirdtButton);
17         ThirdButton.setOnClickListener(new ButtonListener());
18     }
19     class ButtonListener implements OnClickListener{
20         public void onClick(View v) {
21             
22              Uri uri = Uri.parse("smsto://0800000123");    
23              Intent it = new Intent(Intent.ACTION_SENDTO, uri);    
24              it.putExtra("sms_body", "The SMS text");    
25              ThirdActivity.this.startActivity(it);   
26         }
27     }
28 }
bubuko.com,布布扣

 ThirdActivity.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     <TextView
 7         android:layout_width="fill_parent"
 8         android:layout_height="wrap_content"
 9         android:text="@string/third"
10     />
11     
12     <Button
13         android:id="@+id/ThirdActivity"
14         android:layout_width="fill_parent"
15         android:layout_height="wrap_content"
16         />
17 
18 </LinearLayout>
bubuko.com,布布扣

AndroidManifest.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.mars.activity06"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="4"
 9         android:targetSdkVersion="18" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.mars.activity06.FirstActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <!--每当程序当中有个Activity的时候就需要使用<activity/>标签对该Activity进行注册       有两个属性name和lable name的值是这个Activity的包名和类名       lable可以自定义-->
26         <activity android:name="com.mars.activity06.SecondActivity" android:label="@string/second"/>
27         <activity android:name="com.mars.activity06.ThirdActivity" android:label="@string/third"/>"
28     </application>
29 
30 </manifest>
bubuko.com,布布扣

 string.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3 
 4     <string name="app_name">Activity06</string>
 5     <string name="action_settings">Settings</string>
 6     <string name="first">FirstActivity</string>
 7     <string name="firstButton">FirstButton</string>
 8     <string name="second">SecondActivity</string>
 9     <string name="secondtButton">SecondButton</string>
10     <string name="third">ThirdActivity</string>
11     <string name="thirdtButton">ThirdButton</string>
12 
13 </resources>
bubuko.com,布布扣

 

 

 

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

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

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

MainActivity.java

bubuko.com,布布扣
 1 import android.app.Activity;
 2 import android.content.Intent;
 3 import android.os.Bundle;
 4 import android.view.View;
 5 import android.view.View.OnClickListener;
 6 import android.widget.Button;
 7 
 8 public class MainActivity extends Activity {
 9     //代表按钮对象的引用
10     private Button myButton;
11     @Override
12     //复写父类当中的onCreate方法,Activity第一次运行时会调用这个方法
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.main);
16         
17         myButton = (Button) findViewById(R.id.myButton);
18         myButton.setOnClickListener(new myButtonListener());
19         
20     }
21     //以下是一个内部类,这个内部类的对象是一个监听器(如果对监听器不是很熟悉,可以参考设计模式当中的观察者模式)
22     class myButtonListener implements OnClickListener{
23 
24         //生成该类的对象,并将其注册到控件上。如果该控件被用户按下,就会执行onClick方法 
25         public void onClick(View v) {
26             
27             Intent intent = new Intent();//生成一个Intent对象
28             
29             intent.putExtra("number", "13112266075");//在Intent对象当中添加一个键值对,键的取名应该加上这个Activity的包名代表着那个包下的数据,格式包名.键的名字,如果不需要传递数据,这步可以不要
30             
31             //设置Intent对象要启动的Activity,不能直接写this,因为直接写this代表的是本类的对象,也就是myButtonListener这个对象
32             /*
33              * setClass函数的第一个参数是一个Context对象
34              * Context是一个类,Activity是Context类的子类,也就是说,所有的Activity对象,都可以向上转型为Context对象
35              * */
36             intent.setClass(MainActivity.this, OtherActivity.class);
37             //通过Intent对象启动另外一个Activity
38             MainActivity.this.startActivity(intent);
39             
40             
41             
42             /*
43             //以下的4行代码将启动发送短信的Activity,
44             Uri uri = Uri.parse("smsto://0800000123");    
45             Intent intent = new Intent(Intent.ACTION_SENDTO, uri);    
46             intent.putExtra("sms_body", "The SMS text");    
47             startActivity(intent);
48          */
49         }
50     }
51 }
bubuko.com,布布扣

 main.xml

bubuko.com,布布扣
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:orientation="vertical"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context=".MainActivity" >
11 
12     <!-- 下面的标签声明了一个Buttin(按钮)控件,并为这个控件设置了ID,这个ID会被注册到R.java文件当中 -->
13     <Button
14         android:id="@+id/myButton"
15         android:layout_width="fill_parent"
16         android:layout_height="wrap_content"
17         android:text="@string/Button"
18         />
19 
20 </LinearLayout>
bubuko.com,布布扣

 OtherActivity.java

bubuko.com,布布扣
 1 import android.app.Activity;
 2 import android.content.Intent;
 3 import android.os.Bundle;
 4 import android.widget.TextView;
 5 
 6 public class OtherActivity extends Activity{
 7     private TextView myTextView;
 8     protected void onCreate(Bundle savedInstanceState) {
 9         
10         super.onCreate(savedInstanceState);
11         setContentView(R.layout.other);
12         //取得从上一个Activity当中传递过来的Intent对象
13         Intent intent = getIntent();
14         //从Intent当中根据key取得value
15         String number = intent.getStringExtra("number");//该方法还有第二个参数,是一个值,表示如果上一个Activity中的键没有值,就将这个方法中的第二个参数赋值number这个变量
16         
17         myTextView = (TextView) findViewById(R.id.myTextView);
18         myTextView.setText(number);
19         
20     }
21     
22 }
bubuko.com,布布扣

 other.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     
 7     
 8     <TextView
 9         android:id="@+id/myTextView"
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12         android:text="@string/TextView"
13         />"
14 
15 </LinearLayout>
bubuko.com,布布扣

 AndriodManifest.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.mars.conver_activity"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="18" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.mars.conver_activity.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         <activity android:name="com.mars.conver_activity.OtherActivity" android:label="@string/OtherActivity"/>"
26     </application>
27 
28 </manifest>
bubuko.com,布布扣

 string.xml

bubuko.com,布布扣
<?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>
bubuko.com,布布扣

 

 

 

MainActivity.java

bubuko.com,布布扣
 1 import android.app.Activity;
 2 import android.content.Intent;
 3 import android.os.Bundle;
 4 import android.view.View;
 5 import android.view.View.OnClickListener;
 6 import android.widget.Button;
 7 
 8 public class MainActivity extends Activity {
 9     private Button button;
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13         
14         button = (Button) findViewById(R.id.button);
15         button.setOnClickListener(new ButtonListener());
16     }
17     class ButtonListener implements OnClickListener{
18         public void onClick(View v) {
19             Intent intent = new Intent();
20             intent.putExtra("com.mars.second_intent.Age", 20);
21             intent.putExtra("com.mars.second_intent.Number", "13112266075");
22             intent.setClass(MainActivity.this, OtherActivity.class);
23             MainActivity.this.startActivity(intent);
24         }
25     } 
26 }
bubuko.com,布布扣

 

activity_main.xml

bubuko.com,布布扣
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <TextView
12         android:id="@+id/textview1"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/hello_world" />
16     
17     <Button 
18         android:id="@+id/button"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:layout_below="@id/textview1"
22         android:text="启动第二个Activity"
23         />
24 
25 </RelativeLayout>
bubuko.com,布布扣

OtherActivity.java

bubuko.com,布布扣
 1 import android.app.Activity;
 2 import android.content.Intent;
 3 import android.os.Bundle;
 4 import android.widget.TextView;
 5 
 6 public class OtherActivity extends Activity{
 7     private TextView textview;
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);
10         setContentView(R.layout.other);
11         Intent intent = getIntent();
12         String str = intent.getStringExtra("com.mars.second_intent.Number");
13         int age = intent.getIntExtra("com.mars.second_intent.Age", 10);
14         
15         textview = (TextView) findViewById(R.id.textview);
16         textview.setText("Number="+str+"age="+age);
17     }
18 }
bubuko.com,布布扣

 

other.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     
 7     
 8     <TextView 
 9         android:id="@+id/textview"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:text="zhognguo"
13         />
14 
15 </LinearLayout>
bubuko.com,布布扣

 

 AndriodManifest.xml

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.mars.second_intent"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="17"
 9         android:targetSdkVersion="18" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.mars.second_intent.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25         
26         <activity 
27             android:name="com.mars.second_intent.OtherActivity"
28             android:label="第二个Activity"
29             ></activity>
30     </application>
31 
32 </manifest>
bubuko.com,布布扣

 

string.xml

bubuko.com,布布扣
<?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>
bubuko.com,布布扣

多个Activity和Intent,布布扣,bubuko.com

多个Activity和Intent

原文:http://www.cnblogs.com/LO-ME/p/3586444.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!