天天看点

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中实现页面跳转并传参

暂时就说到这吧,后续也会补充新的方法。