分享

Android从零开始(11)(Activity跳转和传参数)(新)

 北斗烛龙 2014-12-25

转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持!


前言

今天要讲的是Activity也是在开发中最常用的,并且大家已经用过了,在刚创建工程,也会自动生成一个Activity,作为默认启动界面,界面是基于Activity,可以把Activity看做一个视图的控制器。

Activity

Activity是Activity的四大组件之一,是应用程序加载布局的容器。(控制中心)

处理用户的响应


意图激活组件

1.显示意图激活(开发常用)

已经明确指定了组件名称的激活方式:显式意图激活

  1. // 跳转(激活另一组件:使用Intent)  
  2. // 得到意图对象  
  3. Intent intent = new Intent();  
  4. // 5种方法都可以  
  5. // 参数:上下文,要激活的对象.class  
  6. intent.setClass(this, OtherActivity.class);  
  7. // 参数:上下文,要激活的Activity类全名  
  8. intent.setClassName(this,"com.cym.preparation.activity.OtherActivity");  
  9. // 参数:ComponentName对象 里面参数:上下文,要激活的对象.class  
  10. intent.setComponent(newComponentName(this,OtherActivity.class));  
  11. // intent的构造参数:上下文,要激活的对象.class  
  12. Intent intent = new Intent(this,OtherActivity.class);  
  13. // 跨应用激活组件 参数:自己的全包名,要激活的Activity类全名  
  14. intent.setClassName("com.cym.preparation.activity","com.cym.preparation.activity.OtherActivity");  
  15. // 设置完成之后开启Activity即可  
  16. startActivity(intent);  

2.隐示意图激活

没有指定组件名称:隐式意图激活。Android系统会根据隐式意图中设置的动作(action)类别(category)数据(uri和数据类别)找到最合适的组件来处理意图

AndroidManifest.xml

<activity .......> 这是你要激活的Activity

Activity最小的intent-filter的配置是这样

必须有一个action 和一个缺省的类别

<intent-filter>

<action android:name=”com.cym.NewActivity”> // name 参数自定义的

<category  android:name=”android.intent.category.DEFAULT”> // 类别有很多常量可以选择

// 还可以配置(不要求一定配置,但是可以配置)

<data android:scheme="hehe"/> // scheme 自定义

<data android:scheme="haha" android:mimeType="hehe/haha"/> // mimeType 要有“自定义/自定义” 这样的格式  一定要有 “/

</intent-filter>

</activity>

Activity:

Intent intent = new Intent();

intent.setAction(“com.cym.NewActivity”) // 填写你自己在你需要激活的Activity配置 acton name 即可 这样就是通过自己定义的名称激活另一个Activity

一个Activity可以配置多个<intent-filter></intent filer>,如果有多个的话intent-filter,通过任意一个都能激活该Activity

如果配置了datascheme属性激活该Activity就一定要配置

intent.setData(Uri.parse("haha:")); // 参数 配置文件scheme 的值 如 haha : 内容后面要加分号

如果配置了datamimetype属性激活该Activity就一定要配置

intent.setType("hehe/haha");

如果同时配置了data 的 scheme 和 mimetype 属性就一定要配置

intent.setDataAndType(Uri.parse("haha:"), "hehe/haha"); // 以上两种data单个配置的方法无效

activity之间参数的传递

1.单个值的之间参数的传递

  1. // 1赋值 Activity1  
  2. Intent intent = new Intent(this,OtherActivity.class);  
  3. // 把数据塞进intent  
  4. // 单个传值  
  5. intent.putExtra("name""邹胖子");  
  6. intent.putExtra("age"18);、  
  7. startActivity(intent);  
  8. // 取值 Activity2  
  9. Intent intent = getIntent();  
  10. // 单个取值  
  11. ntent.getStringExtra("name") + ":" + intent.getIntExtra("age"0)  
  12. ------------------------------------------------------------------------------------  
  13. // 2批量传值  Activity1  
  14.   Bundle bundle =        new Bundle();  
  15.   bundle.putString("name""Cym");  
  16.   bundle.putInt("age"18);  
  17.   intent.putExtras(bundle);  
  18. startActivity(intent);  
  19. // 批量取值 Activity2   用以上单个取值方式也可以  
  20. Intent intent = getIntent();  
  21. Bundle bundle = intent.getExtras();  
  22. bundle.getString("name") + ":" + bundle.getInt("age")  


2对象之间参数的传递

  1. //1将实体实现Serializable (序列化) :public class student implements Serializable  
  2. //1对象传值;Activity1  
  3. student stu = new student("FFF",11);  
  4. intent.putExtra("stu", stu);  // 参数:需要传一个 Serializable 的类  
  5. //2 对象取值Activity2  
  6. Intent intent = getIntent();  
  7. student stu = (student) intent.getSerializableExtra("stu");  
  8. String name = stu.name;  
  9. int age = stu.age;  
  10. -------------------------------------------------------------------  
  11. //1将实体实现Parcelable (包装类)  
  12. Domain:  
  13. public int id;  
  14. public String name;  
  15. public int age;  
  16. @Override  
  17.         public int describeContents() {  
  18.                 // TODO Auto-generated method stub  
  19.                 return 0;  
  20.         }  
  21.         //把对象的属性保存在Parcel对象里面  
  22.         @Override  
  23.         public void writeToParcel(Parcel dest, int flags) {  
  24.                 // TODO Auto-generated method stub  
  25.         dest.writeInt(id);  
  26.         dest.writeString(name);  
  27.         dest.writeInt(age);  
  28.         }  
  29. public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {  
  30.                  
  31.                 //从Parcel对象里面获取属性,在构造出对象  
  32.                 public Student createFromParcel(Parcel in) {  
  33. // 获取属性顺序要按属性保存顺序  
  34.                         int id = in.readInt();  
  35.                         String name = in.readString();  
  36.                         int age= in.readInt();  
  37.                         return new Student(id,name,age);  
  38.                 }  
  39.                 public Student[] newArray(int size) {  
  40.                         return new Student[size];  
  41.                 }  
  42.         };  
  43. //2对象传值 Activity1  
  44.              person p = new person("Cym"20);  
  45.             intent.putExtra("person", p);  
  46. // 对象取值 Activity2  
  47. person person = (person) intent.getParcelableExtra("person");  
  48.         tv_show.setText(person.name + "-" + person.age);  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多