使用Intent實現Activity的顯式跳轉

【正文】java

這裏以按鈕實現活動跳轉爲例,爲實現這個功能,咱們須要三個步驟:android

1.點擊按鈕才發生頁面跳轉,所以,第一步咱們先要找到要點擊的按鈕app

如何拿到按鈕對象呢?經過資源id,前面咱們提到過,在R.id.xxx 中會有咱們的資源id,但button按鈕是在layout 中建立的,系統不會爲其建立資源id,因此咱們須要在layout 設置 button 時本身加上id,、,具體方法以下:ide

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。ui

 

2.找到按鈕以後,點擊按鈕以後纔會發生跳轉,因此咱們須要給這個按鈕綁定事件監聽器this

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

 

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);
            }
        });
    }
}

 

 

 

 

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

這裏咱們以按鈕實現活動跳轉爲例,爲實現這個功能,咱們須要三個步驟:.net

一、拿到按鈕對象

       如何拿到按鈕對象呢?經過資源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。

 

二、爲此按鈕設定點擊監聽事件

這樣每當點擊按鈕時,就會執行監聽器中的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. }  


三、實現跳轉

      固然這是最重要的一步了,經過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有六種構造函數,三、四、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);  

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

相關文章
相關標籤/搜索