1、Android项目结构
manifests 安卓的全局描述文件 application文件Java源码文件 显示所有包和源文件 R文件 生成索引,如果R文件生成失败,用Clean Project com.mingrisoft:放置java源文件res资源文件 1.drawable目录 2.layout布局文件 3.mipmap子目录:mipmap-mdpi、mipmap-hdpi等 4.values:保存样式资源,颜色资源,尺寸资源
2、VIEW以及常用属性
视图,提供组件配置和视图的方法
android:id属性
Android:background属性
Android:padding属性 设置内边距
2.1 ViewGroup
ViewGroup.LayoutParams类ViewGroup.MarginLayoutParams类 :设置外边距
2.2 控制UI界面
使用XML布局文件控制UI界面 在Android应用的res/layout目录下编写XML布局文件 在Activity中使用以下Java代码显示XML文件中的布局内容
setContentView(R.layout.activity_main);
在Java代码中控制UI界面使用XML和Java代码混合控制UI界面开发自定义的View
3、传递参数
Android开发中,在不同模块(如Activity)间经常会有各种各样的数据需要相互传递
1. 利用Intent对象携带简单数据
利用Intent的Extra部分来存储我们想要传递的数据,可以传送String , int, long, char等一些基础类型,不能传输复杂的参数
3.1.1设置参数
//传递些简单的参数
Intent intent1 = new Intent();
intent1.setClass(MainActivity.this,SimpleActivity.class);
//intent1.putExtra("usr", "lyx");
//intent1.putExtra("pwd", "123456");
//startActivity(intent1);
Bundle bundleSimple = new Bundle();
bundleSimple.putString("usr", "lyx");
bundleSimple.putString("pwd", "123456");
intent1.putExtras(bundleSimple);
startActivity(intent1);
3.1.2 接收参数
//接收参数
//Intent intent = getIntent();
//String eml = intent.getStringExtra("usr");
//String pwd = intent.getStringExtra("pwd");
Bundle bundle = this.getIntent().getExtras();
String eml = bundle.getString("usr");
String pwd = bundle.getString("pwd");
3.2 利用Intent对象携带如ArrayList之类复杂些的数据
3.2.1 设置参数
//传递复杂些的参数
Map<String, Object> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
List<Map<String, Object>> list = new ArrayList<>();
list.add(map);
Intent intent = new Intent();
intent.setClass(MainActivity.this,ComplexActivity.class);
Bundle bundle = new Bundle();
//须定义一个list用于在budnle中传递需要传递的ArrayList<Object>,这个是必须要的
ArrayList bundlelist = new ArrayList();
bundlelist.add(list);
bundle.putParcelableArrayList("list",bundlelist);
intent.putExtras(bundle);
startActivity(intent);
3.2.2 接收参数
this.setTitle("复杂参数传递例子");
//接收参数
Bundle bundle = getIntent().getExtras();
ArrayList list = bundle.getParcelableArrayList("list");
//从List中将参数转回 List<Map<String, Object>>
List<Map<String, Object>> lists= (List<Map<String, Object>>)list.get(0);
String sResult = "";
for (Map<String, Object> m : lists) {
for (String k : m.keySet()) {
sResult += "\r\n" + k + " : " + m.get(k);
}
}
TextView tv = findViewById(R.id.tv);
tv.setText(sResult);
3.3 通过实现Serializable接口
Java语言本身的特性,通过将数据序列化后,再将其传递出去。 实体类:!
public class Person implements Serializable {
private String name;
private int 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;
}
}
3.3.1 设置参数
//1.通过Serializable接口传参数的例子
//HashMap<String,String> map2 = new HashMap<>();
//map2.put("key1", "value1");
//map2.put("key2", "value2");
//Bundle bundleSerializable = new Bundle();
//bundleSerializable.putSerializable("serializable", map2);
//Intent intentSerializable = new Intent();
//intentSerializable.putExtras(bundleSerializable);
//intentSerializable.setClass(MainActivity.this,
//SerializableActivity.class);
//startActivity(intentSerializable);
//2.通过Serializable接口传递实体类
Person person = new Person();
person.setAge(25);
person.setName("lyx");
Intent intent2 = new Intent(MainActivity.this, SerializableActivity.class);
intent2.putExtra("serializable", person);
startActivity(intent2);
3.3.2 接收参数
this.setTitle("Serializable例子");
//接收参数
//1.接收集合
// Bundle bundle = this.getIntent().getExtras();
//如果传 LinkedHashMap,则bundle.getSerializable转换时会报ClassCastException,不知道什么原因
//传HashMap倒没有问题。
//HashMap<String, String> map = (HashMap<String, String>) bundle.getSerializable("serializable");
//
//String sResult = "map.size() =" + map.size();
//
//Iterator iter = map.entrySet().iterator();
// while (iter.hasNext()) {
// Map.Entry entry = (Map.Entry) iter.next();
// Object key = entry.getKey();
// Object value = entry.getValue();
// //System.out.println("key---->"+ (String)key);
// //System.out.println("value---->"+ (String)value);
//
// sResult += "\r\n key----> " + (String) key;
// sResult += "\r\n value----> " + (String) value;
// }
//2.接收实体类
Person person = (Person) getIntent().getSerializableExtra("serializable");
String sResult = "姓名:" + person.getName() + "--年龄:" + person.getAge();
TextView tv = findViewById(R.id.tv);
tv.setText(sResult);
3.4 通过实现Parcelable接口
这个是通过实现Parcelable接口,把要传的数据打包在里面,然后在接收端自己分解出来。这个是Android独有的,在其本身的源码中也用得很多,效率要比Serializable相对要好。 Parcelable方式实现的原理是将一个完整的对象进行分解 , 而分解后的每一部分都是Intent所支持的数据类型 , 这样也就实现传递对象的功能了 .
Parcelable的实现方式 :
public class Student implements Parcelable {
private String name;
private int age;
public Student() {
}
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 Student(Parcel in) {
name = in.readString();
age = in.readInt();
}
public static final Creator<Student> CREATOR = new Creator<Student>() {
@Override
public Student createFromParcel(Parcel in) {
return new Student(in);
}
@Override
public Student[] newArray(int size) {
return new Student[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(age);
}
}
Parcelable的实现方式要稍微复杂一些 , 首先让Student类去实现了Parcelable接口 , 这样就必须重写describeContents()和writeToParcel()这两个方法 . 其中describeContents()方法可以直接返回0就可以了 , 而writeToParcel()方法中需要调用Parcel的writeXxx()方法 , 将Student类中的字段一一写出 . 注意 , 字符串数据就调用writeString()方法 , 整数数据就调用writeInt()方法 , 一次类推 . 除此之外 , 我们还必须在Student类中提供一个名为CREATOR的常量 , 这里创建了Parcelable.Creator接口的一个实现 , 并将泛型指定为Student . 接着需要重写createFromParcel()和newArray()这两个方法 . 在createFromParcel()方法中我们要去创建一个Student对象 , .而newArray()方法只需要new出一个Student数组 , 并传入size()作为数据的大小就可以了 .
4.1 设置参数
Student student = new Student();
student.setAge(25);
student.setName("lyx");
Intent intent4 = new Intent(MainActivity.this, ParcelableActivity.class);
intent4.putExtra("parcelable", student);
startActivity(intent4);
4.2 接收参数
this.setTitle("Parcelable例子");
//接收参数
Intent i = getIntent();
Student student = i.getParcelableExtra("parcelable");
TextView tv = findViewById(R.id.tv);
tv.setText("姓名:" + student.getName() + "--年龄:" + student.getAge());
转自:https://blog.csdn.net/xcl168/article/details/14436539