网站首页 极客学院 视频课堂 极客论坛 下载专区 行业新闻 欢迎光临极客研究院,今天是2019-08-24 星期六
会员面板
帐号:
密码:
验证码:
极客学院
教程搜索
输入要搜索的内容:

下载专区
精品教程
   网站首页 > 移动开发 > Android开发教程 > Intent传递参数的多种方法

Intent传递参数的多种方法

Intent实现页面跳转和传值


*Intent称为意图,是Android各大组件连接的桥梁


1.Activity页面跳转


同一个包内


Intent intent = new Intent();

intent.setClass(MainActivity.this, SecondActivity.class);  

MainActivity.this.startActivity(intent);


不同包内


(1)明确的Intent


Intent intent = new Intent();

ComponentName comp = new ComponentName("包名(应用本身所在的包,通过AndroidManifest.xml中package属性查看)","包名.类名(Activity所在的包)");

//或者intent.setClassName("包名","包名.类名");

intent.setComponent(comp);

startActivity(intent);


(2)不明确的Intent


Intent intent = new Intent();

intent.setAction(Intent.ACTION_MAIN); // 应用程序的入口

intent.addCategory(Intent.CATEGORY_HOME); // 桌面的应用程序

startActivity(intent);


然后在AndroidManifest.xml中注册该过滤条件

复制代码


<activity

   android:name=".OtherActivity"

   android:label="OtherActivity" >

   <intent-filter>

       <action android:name="action_name" />

       <category android:name="android.intent.category.DEFAULT" />

   </intent-filter>

</activity>


复制代码


注:


*第一个Activity是外部通过不明确的Intent跳转的

*要设置第一个被启动的Activity,需要设置如下属性


<intent-filter>

   <action android:name="android.intent.action.MAIN" />

   <category android:name="android.intent.category.LAUNCHER" />

</intent-filter>


2.Activity页面跳转传值


第一种方法:(Intent)


发送方:


Intent intent = new Intent();

intent.putExtra("name", "诸葛亮");

intent.putExtra("age", 50);

intent.putExtra("IQ", 200.0f);

intent.setClass(MainActivity.this, SecondActivity.class);

MainActivity.this.startActivity(intent);


接受方:


Intent intent = getIntent();

String name = intent.getStringExtra("name");

int age = intent.getIntExtra("age", 0);

float IQ = intent.getFloatExtra("IQ", 0.0f);

textview2.setText("name:"+name+",age:"+age+",IQ:"+IQ);


第二种方法:(Bundle)


发送方:

复制代码


Intent intent = new Intent();

Bundle bundle = new Bundle();

bundle.putString("name", "乔峰");

bundle.putInt("age", 40);

bundle.putFloat("weight", 70.4f);

intent.putExtras(bundle);

intent.setClass(MainActivity.this, SecondActivity.class);

startActivity(intent);


复制代码


接受方:


Intent intent = getIntent();

Bundle bundle = intent.getExtras();

String name = bundle.getString("name");

int age = bundle.getInt("age");

float weight = bundle.getFloat("weight");

textview.setText(name+","+age+","+weight);


第三种方法:(Application)


首先要新建一个继承Application的MyApp类,补充属性的get/set方法


AndroidManifest.xml也要配置application的属性android:name=".MyApp"


发送方:


MyApp myApp = (MyApp) getApplication();

myApp.setName("周星驰");

Intent intent = new Intent();

intent.setClass(MainActivity.this, SecondActivity.class);

startActivity(intent);


接受方:


MyApp myApp = (MyApp) getApplication();

String name = myApp.getName();

textview = (TextView) findViewById(R.id.textview);

textview.setText(name);


第四种方法:(推荐)


对象类要实现Parclelable接口


发送方:


Intent intent = new Intent();

Person person = new Person("孙悟空",500,55.6f);

intent.putExtra("person", person);

intent.setClass(MainActivity.this, SecondActivity.class);

startActivity(intent);


接受方:


Intent intent = getIntent();

Person person = intent.getParcelableExtra("person");

textview = (TextView) findViewById(R.id.textview);

textview.setText(person.toString());


对象类:

复制代码


public class Person implements Parcelable {

   private String name;

   private int age;

   private float weight;


   public Person() {

       super();

       // TODO Auto-generated constructor stub

   }


   public Person(String name, int age, float weight) {

       super();

       this.name = name;

       this.age = age;

       this.weight = weight;

   }


   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;

   }


   public float getWeight() {

       return weight;

   }


   public void setWeight(float weight) {

       this.weight = weight;

   }


   @Override

   public String toString() {

       return "Person [name=" + name + ", age=" + age + ", weight=" + weight + "]";

   }


   @Override

   public int describeContents() {

       return 0;

   }


   @Override

   public void writeToParcel(Parcel dest, int flags) {

       dest.writeString(name);

       dest.writeInt(age);

       dest.writeFloat(weight);

   }


   public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>() {

       public Person createFromParcel(Parcel in) {

           return new Person(in);

       }


       public Person[] newArray(int size) {

           return new Person[size];

       }

   };


   private Person(Parcel in) {

       name = in.readString();

       age = in.readInt();

       weight = in.readFloat();

   }

}


复制代码


3.页面返回传值


被返回方:


startActivityForResult(intent, 38);


复制代码


@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

   Bundle bundle = data.getExtras();

   String name = bundle.getString("name");

   int age = bundle.getInt("age");

   float weight = bundle.getFloat("weight");

   Toast.makeText(MainActivity.this, name+age+weight, Toast.LENGTH_LONG).show();

   super.onActivityResult(requestCode, resultCode, data);

   }


复制代码


返回方:

复制代码


Intent data = new Intent();

data.setClass(SecondActivity.this, MainActivity.class);

Bundle bundle = new Bundle();

bundle.putString("name", "张无忌");

bundle.putInt("age", 20);

bundle.putFloat("weight", 120.5f);

data.putExtras(bundle);

setResult(250, data);

finish();


复制代码




启动Activity


上下文中,通过如下方法启动Activity

*startActivity(Intent intent):启动新的Activity

*startActivityForResult(Intent intent, int requestCode):指定请求码启动新的Activity


如果使用startActivityForResult()启动Activity,则必须重写onActivityResult()方法,并且该方法第一个参数与requestCode相对应


关闭Activity


上下文中,通过如下方法关闭Activity

*finish():关闭当前Activity

*finishActivity(int requestCode):关闭以startActivity启动的Activity

在调用finish()关闭当前Activity前,可以调用setResult(int resultCode)设置返回码。返回码在启动的Activity中通过onActivityResult()捕获

  相关文章
本文标题:Intent传递参数的多种方法
对本文有疑问?现在就进入论坛提问>>
 
粤ICP备18090445号