Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 技術基礎

第69章、初識 Intent — 開啟另一個 Activity 並單向傳值(從零開始學 Android)

編輯:Android 技術基礎

在 Android 應用中,Activity 之間的跳轉是透過 Intent 機制完成的。本案例將簡單介紹如何運用 Intent,讓程式由 MainActivity 跳轉到另一個 OtherActivity,並實現由前者向後者的「單向傳值」。

一、設計介面

1. MainActivity 版面配置

開啟 res/layout/activity_main.xml:

[html] view plain copy    
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout   
  3.     xmlns:android="https://schemas.android.com/apk/res/android"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <Button  
  9.         android:id="@+id/open"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="打開另一個Activity" />  
  13.   
  14. </LinearLayout>  

2. OtherActivity 版面配置

開啟 res/layout/otheractivity.xml:

[html] view plain copy    
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/close"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="關閉當前Activity(OtherActivity)" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/orderno"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="顯示從MainActivity的傳值!" />  
  18.   
  19. </LinearLayout>  

二、程式碼實作

1. MainActivity.java

開啟 MainActivity.java,透過 putExtra 將資料放入 Intent 再啟動 OtherActivity:

[java] view plain copy    
  1. package com.genwoxue.intent_a;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.   
  15. private Button btnOpen=null;  
  16. @Override  
  17. public void onCreate(Bundle savedInstanceState)     
  18.     {     
  19.         super.onCreate(savedInstanceState);                
  20.         setContentView(R.layout.activity_main);      
  21.         btnOpen=(Button)super.findViewById(R.id.open);  
  22.         btnOpen.setOnClickListener(new OnClickListener(){  
  23.         public void onClick(View v)  
  24.         {    
  25.         Intent intent=new Intent(MainActivity.this,OtherActivity.class);  
  26.         intent.putExtra("info", "No66778899");  
  27.         MainActivity.this.startActivity(intent);  
  28.         }  
  29.         });  
  30.           
  31.     }  
  32. }          
  33.       
  34.       

2. OtherActivity.java

開啟 OtherActivity.java,以 getStringExtra 接收並顯示 MainActivity 傳來的值:

[java] view plain copy    
  1. package com.genwoxue.intent_a;  
  2.   
  3.   
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.content.Intent;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.TextView;  
  11.   
  12.   
  13. public class OtherActivity extends Activity {  
  14.   
  15.   
  16. private TextView tvOrderNo=null;  
  17. private Button btnClose=null;  
  18. @Override  
  19. public void onCreate(Bundle savedInstanceState)     
  20.     {     
  21.         super.onCreate(savedInstanceState);                
  22.         setContentView(R.layout.otheractivity);             
  23.         tvOrderNo=(TextView)super.findViewById(R.id.orderno);  
  24.         btnClose=(Button)super.findViewById(R.id.close);  
  25.           
  26.         Intent intent=super.getIntent();  
  27. String orderNo=intent.getStringExtra("info");  
  28. tvOrderNo.setText(orderNo);  
  29. btnClose.setOnClickListener(new OnClickListener(){  
  30.         public void onClick(View v)  
  31.         {    
  32.         finish();  
  33.         }  
  34.         });  
  35.     }  
  36. }            
  37.       

三、設定檔

開啟 AndroidManifest.xml,記得將 OtherActivity 註冊進來:

[html] view plain copy    
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="https://schemas.android.com/apk/res/android"  
  3.     package="com.genwoxue.intent_a"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="15" />  
  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.genwoxue.intent_a.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.        <strong><span style="color:#ff0000;"> <activity  
  27.             android:name="com.genwoxue.intent_a.OtherActivity">  
  28.         </activity>  
  29. </span></strong>    </application>  
  30.   
  31. </manifest>  

四、執行結果

 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved