使用Intent實現Activity的顯式跳轉


【正文】

這里以按鈕實現活動跳轉為例,為實現這個功能,我們需要三個步驟:

1.點擊按鈕才發生頁面跳轉,因此,第一步我們先要找到要點擊的按鈕

如何拿到按鈕對象呢?通過資源id,前面我們提到過,在R.id.xxx 中會有我們的資源id,但button按鈕是在layout 中創建的,系統不會為其創建資源id,所以我們需要在layout 設置 button 時自己加上id,、,具體方法如下:

Activity_main.xml中

<Button  
       android:id="@+id/button1"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:text="點我點我!"   
       android:textSize="25sp"/>  

可以看到設置id 的方法是 id = "@+id/button1",這里button1 即我們將使用的資源id。

 

2.找到按鈕之后,點擊按鈕之后才會發生跳轉,所以我們需要給這個按鈕綁定事件監聽器

3.當有點擊事件產生后,事件監聽器就會監聽到點擊事件,然后去回調事件監聽其中的onClick方法實現跳轉

 

package cn.com.farsight.activity02;

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 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /*
         * 點擊一個按鈕,完成從一個頁面跳轉到另外一個頁面
         */

        // 1.點擊按鈕才發生頁面跳轉,因此,第一步我們先要找到要點擊的按鈕
        Button button = (Button) findViewById(R.id.button1);

        // 2.找到按鈕之后,點擊按鈕之后才會發生跳轉,所以我們需要給這個按鈕綁定事件監聽器

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //3.當有點擊事件產生后,事件監聽器就會監聽到點擊事件,然后去回調事件監聽其中的onClick方法
                //在這里,我們就需要完成頁面跳轉了
                //構建了一個Intent對象,來完成頁面跳轉
                Intent intent = new Intent(MainActivity.this, Second.class);
                startActivity(intent);
            }
        });
    }
}

 

 

 

 

二、使用 Intent 實現活動的顯示跳轉

這里我們以按鈕實現活動跳轉為例,為實現這個功能,我們需要三個步驟:

1、拿到按鈕對象

       如何拿到按鈕對象呢?通過資源id,前面我們提到過,在R.id.xxx 中會有我們的資源id,但button按鈕是在layout 中創建的,系統不會為其創建資源id,所以我們需要在layout 設置 button 時自己加上id,、,具體方法如下:

[java]  view plain  copy
 
  1. <Button  
  2.        android:id="@+id/button1"  
  3.        android:layout_width="match_parent"  
  4.        android:layout_height="wrap_content"  
  5.        android:text="點我點我!"   
  6.        android:textSize="25sp"/>  

可以看到設置id 的方法是 id = "@+id/button1",這里button1 即我們將使用的資源id。

 

2、為此按鈕設定點擊監聽事件

這樣每當點擊按鈕時,就會執行監聽器中的onClick()方法,我們只需要在這個方法中加入待處理的邏輯就行了;

具體代碼如下:

[java]  view plain  copy
 
  1. public class MainActivity extends Activity {  
  2.     private Button button;  
  3.       
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_main);  
  8.           
  9.         button = (Button) findViewById(R.id.button);  
  10.           
  11.         button.setOnClickListener(new OnClickListener() {  
  12.             @Override  
  13.             public void onClick(View v) {  
  14.             // 在此處添加邏輯  
  15.             }  
  16.         });  
  17.     }  
  18. }  


3、實現跳轉

      當然這是最重要的一步了,通過Intent 實現,我們先來了解一下Intent 函數;

Intent  意圖,告訴系統我們要干什么,連接四大組件的紐帶,可以啟動活動、啟動服務、發送廣播;

公共構造函數:

1)、Intent() 空構造函數

2)、Intent(Intent o) 拷貝構造函數

3)、Intent(String action) 指定action類型的構造函數

4)、Intent(String action, Uri uri) 指定Action類型和Uri的構造函數,URI主要是結合程序之間的數據共享ContentProvider

5)、Intent(Context packageContext, Class<?> cls) 傳入組件的構造函數,也就是上文提到的

6)、Intent(String action, Uri uri, Context packageContext, Class<?> cls) 前兩種結合體

Intent有六種構造函數,3、4、5是最常用的,並不是其他沒用!

Intent(String action, Uri uri)  的action就是對應在AndroidMainfest.xml中的action節點的name屬性值。在Intent類中定義了很多的Action和Category常量。

 

下面,我們來具體實現:

1)、創建Intent 對象

[java]  view plain  copy
 
  1. Intent intent = new Intent();  

2)、把我們的意圖封裝進Intent 對象中

這里我們需要先了解 context :應用程序上下文,就是表示當前對象的一個語境,訪問全局信息 的API

這里使用了Intent 的 setclass 方法,我們來看看其定義:

[java]  view plain  copy
 
  1. /** 
  2.     * Convenience for calling {@link #setComponent(ComponentName)} with the 
  3.     * name returned by a {@link Class} object. 
  4.     * 
  5.     * @param packageContext A Context of the application package implementing 
  6.     * this class. 
  7.     * @param cls The class name to set, equivalent to 
  8.     *            <code>setClassName(context, cls.getName())</code>. 
  9.     * 
  10.     * @return Returns the same Intent object, for chaining multiple calls 
  11.     * into a single statement. 
  12.     * 
  13.     * @see #setComponent 
  14.     */  
  15.    public Intent setClass(Context packageContext, Class<?> cls) {  
  16.        mComponent = new ComponentName(packageContext, cls);  
  17.        return this;  
  18.    }  

這里 packageContext 即我們現在的 activity ,而Class<?> cls 則是我們的目的activity ,我們看看具體實現:

[java]  view plain  copy
 
  1. intent.setClass(MainActivity.this,SecondActivity.class);  

 

3)告訴系統執行操作

[java]  view plain  copy
 
  1. startActivity(intent);  

實現這三步就能基本實現活動的跳轉了;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM