1. 程式人生 > >Android中activity和xml的第一個專案

Android中activity和xml的第一個專案

我們使用的手機不光是隻用到一個應用程式,比如在淘寶介面要付款的時候可能會啟動微信付款等,這就相當於在淘寶的Activity中啟動了微信的Activity。還比如說當我們註冊一個網站是,可能會給自己傳送一條簡訊作為驗證,這就是在當前的Activity中啟動了簡訊的Activity。之前對比的MVC設計模式中Controller可以呼叫另一個Controller中的資料或者跳轉等,那麼在Android平臺中也是可以實現Activity之間的呼叫的。

 

程式:

              

首先做這樣一個程式,從一個Activity跳轉到另一個Activity中:

              關聯的佈局檔案main.xml:

[html]  view plain  copy
  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="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.       
  7.     <Button  
  8.         android:id="@+id/myButton"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         />  
  12.   
  13. </LinearLayout>  

              第一個Activity:

[csharp]  view plain  copy
  1. public class Activity extends ActionBarActivity {  
  2.     /** 
  3.      * Called when the activity is first created 
  4.      */  
  5.     //首先獲得點選跳轉的按鈕  
  6.     private Button myButton =null;  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.main); //選擇關聯的佈局檔案  
  11.         myButton=(Button)findViewById(R.id.myButton); //通過id獲得按鈕  
  12.         //通過監聽器把物件捆綁到按鈕上  
  13.         myButton.setOnClickListener((android.view.View.OnClickListener) new MyButtonListener());  
  14.     }  
  15.       
  16.     //監聽器類  
  17.     class MyButtonListener implements OnClickListener{    
  18.         public void onClick(View v){  
  19.             // 生成一個Intent物件  
  20.             Intent intent=new Intent();  
  21.             intent.putExtra("testIntent""123");  
  22.             intent.setClass(Activity.this, OtherActivity.class); //設定跳轉的Activity  
  23.             Activity.this.startActivity(intent);  
  24.         }  
  25.     }  
  26. }  

              第一個Activity到第二個Activity是通過intent來傳送資料的,那麼在第二個Activity是如何接收資料的呢?第二個Activity對應的佈局檔案other.xml中只用一個TextView來盛放資料。

[html]  view plain  copy
  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="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.       
  7.     <TextView  
  8.         android:id="@+id/myTextView"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         />  
  12. </LinearLayout>  

              第二個Activity中接收資料

[csharp]  view plain  copy
  1. public class OtherActivity extends Activity {  
  2.     private TextView myTextView=null;  
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         // TODO Auto-generated method stub  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.other);  
  8.         Intent intent=getIntent();  
  9.         String value=intent.getStringExtra("testIntent");  
  10.         myTextView=(TextView)findViewById(R.id.myTextView);  
  11.         myTextView.setText(value);  
  12.     }  
  13. }  

             當然不要忘記在配置檔案中註冊第二個Activity:

[html]  view plain  copy
  1. <activity android:name=".OtherActivity" android:label="@string/other" />  

              在一個Activity跳轉到另外一個Activity中是通過intent物件來傳遞的,而一個Intent物件到底能包含多少東西還需要我們自己去實踐。