转载请注明本文出自Cym的博客(http://blog.youkuaiyun.com/cym492224103),谢谢支持!
前言
今天要讲的是Activity也是在开发中最常用的,并且大家已经用过了,在刚创建工程,也会自动生成一个Activity,作为默认启动界面,界面是基于Activity,可以把Activity看做一个视图的控制器。
Activity
Activity是Activity的四大组件之一,是应用程序加载布局的容器。(控制中心)
处理用户的响应
意图激活组件
1.显示意图激活(开发常用)
已经明确指定了组件名称的激活方式:显式意图激活
// 跳转(激活另一组件:使用Intent)
// 得到意图对象
Intent intent = new Intent();
// 5种方法都可以
// 参数:上下文,要激活的对象.class
intent.setClass(this, OtherActivity.class);
// 参数:上下文,要激活的Activity类全名
intent.setClassName(this,"com.cym.preparation.activity.OtherActivity");
// 参数:ComponentName对象 里面参数:上下文,要激活的对象.class
intent.setComponent(newComponentName(this,OtherActivity.class));
// intent的构造参数:上下文,要激活的对象.class
Intent intent = new Intent(this,OtherActivity.class);
// 跨应用激活组件 参数:自己的全包名,要激活的Activity类全名
intent.setClassName("com.cym.preparation.activity","com.cym.preparation.activity.OtherActivity");
// 设置完成之后开启Activity即可
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。
如果配置了data的scheme属性激活该Activity就一定要配置
intent.setData(Uri.parse("haha:")); // 参数 配置文件scheme 的值 如 haha : 内容后面要加分号
如果配置了data的mimetype属性激活该Activity就一定要配置
intent.setType("hehe/haha");
如果同时配置了data 的 scheme 和 mimetype 属性就一定要配置
intent.setDataAndType(Uri.parse("haha:"), "hehe/haha"); // 以上两种data单个配置的方法无效
activity之间参数的传递
1.单个值的之间参数的传递
// 1赋值 Activity1
Intent intent = new Intent(this,OtherActivity.class);
// 把数据塞进intent
// 单个传值
intent.putExtra("name", "邹胖子");
intent.putExtra("age", 18);、
startActivity(intent);
// 取值 Activity2
Intent intent = getIntent();
// 单个取值
ntent.getStringExtra("name") + ":" + intent.getIntExtra("age", 0)
------------------------------------------------------------------------------------
// 2批量传值 Activity1
Bundle bundle = new Bundle();
bundle.putString("name", "Cym");
bundle.putInt("age", 18);
intent.putExtras(bundle);
startActivity(intent);
// 批量取值 Activity2 用以上单个取值方式也可以
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
bundle.getString("name") + ":" + bundle.getInt("age")
2对象之间参数的传递
//1将实体实现Serializable (序列化) :public class student implements Serializable
//1对象传值;Activity1
student stu = new student("FFF",11);
intent.putExtra("stu", stu); // 参数:需要传一个 Serializable 的类
//2 对象取值Activity2
Intent intent = getIntent();
student stu = (student) intent.getSerializableExtra("stu");
String name = stu.name;
int age = stu.age;
-------------------------------------------------------------------
//1将实体实现Parcelable (包装类)
Domain:
public int id;
public String name;
public int age;
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
//把对象的属性保存在Parcel对象里面
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeInt(id);
dest.writeString(name);
dest.writeInt(age);
}
public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() {
//从Parcel对象里面获取属性,在构造出对象
public Student createFromParcel(Parcel in) {
// 获取属性顺序要按属性保存顺序
int id = in.readInt();
String name = in.readString();
int age= in.readInt();
return new Student(id,name,age);
}
public Student[] newArray(int size) {
return new Student[size];
}
};
//2对象传值 Activity1
person p = new person("Cym", 20);
intent.putExtra("person", p);
// 对象取值 Activity2
person person = (person) intent.getParcelableExtra("person");
tv_show.setText(person.name + "-" + person.age);