天天看點

Android中實作頁面跳轉并傳參

關于頁面跳轉,想必大家都不陌生,具體操作如下:

首先得有兩個activity,姑且取個名字,MainActivity和JumpActivity,如何從MainActivity跳轉到JumpActivity呢?

其實就是通過Intent實作的,就是下面這樣:

// 其中MainActivity是目前頁面,JumpActivity是要跳轉的頁面
Intent intent = new Intent(MainActivity.this,JumpActivity.class);
startActivity(intent);
           

方法很簡單,就兩行代碼的事,但是,有一點需要注意的是,一定要在AndroidManifest.xml中進行注冊啊!

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoBar"
    android:windowSoftInputMode="stateAlwaysHidden">
       <intent-filter>
           <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
       </intent-filter>
 </activity>
 <activity android:name=".JumpActivity" />
           

當然了,如果自己建立了包,記得把路徑改了,至于裡面的屬性,這裡就不再細說了,我們這次的重點是傳參。

1. 簡單參數傳遞

利用Intent的Extra部分來存儲我們想要傳遞的資料,這可能是最簡單的一種方法了。

1.1 設定參數

Intent intent = new Intent(MainActivity.this,JumpActivity.class);
// 設定你要傳的參數
intent.putExtra("name", "hsl");
startActivity(intent);
           

1.2 接收參數

// 使用Intent對象得到MainActivity中傳過來的參數
Intent intent = getIntent();
String list = intent.getStringExtra("name");
           

這裡需要提一下的是,我們使用的是intent直接傳遞資料,還有一種方法是通過bundle來實作的,其實,兩種方式差别不大:

1.3 bundle設定參數

Intent intent = new Intent(MainActivity.this,JumpActivity.class);
// 設定你要傳的參數
Bundle bundle = new Bundle();
bundle.putString("name", "hsl");
intent.putExtras(bundle);
startActivity(intent);
           

1.4 bundle接收參數

Bundle bundle = this.getIntent().getExtras();
String data= bundle.getString("name");
           

2. 通過實作Parcelable接口

Parcelable方式實作的原理是将一個完整的對象進行分解 ,進而達到執行個體化對象的目的。實作Parcelable接口的類還必須有一個名為CREATOR的靜态字段,它是實作Parcelable的對象。

2.1 自定義MyParcelable

其實這個可以跳過,因為我們一般自定義的類會繼承自帶的Parcelable,不用自己去定義,當然,自定義的也是沒有問題的。

public class MyParcelable implements Parcelable {
     private int mData;

     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(mData);
     }

     public static final Parcelable.Creator<MyParcelable> CREATOR
             = new Parcelable.Creator<MyParcelable>() {
         public MyParcelable createFromParcel(Parcel in) {
             return new MyParcelable(in);
         }

         public MyParcelable[] newArray(int size) {
             return new MyParcelable[size];
         }
     };

     private MyParcelable(Parcel in) {
         mData = in.readInt();
     }
 }
           

2.2 自定義類

public class Person implements Parcelable {

    private String name;
    private int age;

    public Person(String name,int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    protected Person(Parcel in) {
        name = in.readString();
        age = in.readInt();
    }

    public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
        @Override
        public Person createFromParcel(Parcel in) {
            return new Person(in);
        }

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(age);
    }
}
           

在writeToParcel這個方法中 , 字元串資料就調用writeString()方法 , 整數資料就調用writeInt()方法 , 其他資料形式類似,總之,要一一對應。

 2.3 設定參數

Person person = new Person("hsl", 18);
Intent intent = new Intent(MainActivity.this,JumpActivity.class);
intent.putExtra("person", person);
startActivity(intent);
           

2.4 接收參數

Person person = (Person) getIntent().getExtras().get("person");
Log.w(TAG,"name:"+person.getName());
Log.w(TAG,"name:"+person.getAge());
           

最後的測試結果:

Android中實作頁面跳轉并傳參

暫時就說到這吧,後續也會補充新的方法。