Android中級教程(二)之調用另一個Activity—Intent對象的使用!

來源:互聯網
上載者:User

 前一個教程介紹了如何運用切換Layout
的方式進行手機頁面間的轉換,如果要轉換的頁面不只是背景,顏色或文字內容的不同,而是Activity
的置換,那,那就不是單單改變Layout
就能完成的,尤其是需要傳遞的變數不像網頁可以通過Cookie
Session
,在程式裡要移交主動權到另外一個Activity
,光靠先前技巧是辦不到的.

 

而下面我們要講的Intent
對象就是為解決這問題而生的,Intent
就如同其英文字義,是"想要"或"意圖",之意,在主Activity
當中,告訴程式自己是什麼,並想要前往哪裡,這就是Intent
對象所處理的事了,本例子和前一個例子我們將實現同一個效果.

 

看一下:

 


 

 

下面是所涉及的代碼:

 

首先是布局main.xml
mylayout.xml

 

main.xml:

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="歡迎來到魏祝林的部落格"
    />
<Button
    android:id="@+id/bt1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="點擊進入Layout2"
/>
</LinearLayout>

 

mylayout.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Welcome to Mr Wei's blog"
    />
<Button
    android:id="@+id/bt2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="點擊進入Layout1"
/>

</LinearLayout>

 

然後是控製程序IntentDemo.java
IntentDemo1.java
代碼:

 

IntentDemo.java:

 

package com.android.test;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class IntentDemo extends Activity {
 
    private Button bt1;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        bt1 = (Button)findViewById(R.id.bt1);
       
        bt1.setOnClickListener(new Button.OnClickListener(){
            public void onClick(View v){
                //new 一個Intent對象,並指定要啟動的Class
                Intent intent = new Intent();             
                intent.setClass(IntentDemo.this, IntentDemo1.class);         
                //調用一個新的Activity
                startActivity(intent);
                //關閉原本的Activity
                IntentDemo.this.finish();


            }
        });
    }
}

 

IntentDemo.java
同一目錄內建立一個IntentDemo1.java

 

IntentDemo1.java:

 

package com.android.test;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;

import android.widget.Button;

public class IntentDemo1
extends Activity {

    private Button bt2;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 載入mylayout.xml
        setContentView(R.layout.mylayout);


        bt2 = (Button) findViewById(R.id.bt2);
        bt2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                // new 一個Intent對象,並指定要啟動的Class
                Intent intent = new Intent();
                intent.setClass(IntentDemo1.this, IntentDemo.class);
                // 調用一個新的Activity
                startActivity(intent);
                // 關閉原本的Activity
                IntentDemo1.this.finish();


            }
        });
    }
}

 

最後是本例子的重點,添加另外一個Activity
所以必須在AndroidManifest.xml
中定義一個新的activty
,並給予名稱name
,或則程式無法編譯運行.新手很容易遇到這個問題.

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".IntentDemo"
                  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="IntentDemo1"></activity>

    </application>
    <uses-sdk android:minSdkVersion="3" />

</manifest>

 

本例子所涉及的的全部代碼已經全部貼出,最後執行之,將達到上述效果!本節到此結束~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.