编写一个Android Studio示例 能够实现Intent传递数组、Intent传递集合、Intent传递对象、Intent传递Bitmap,给出详细代码
时间: 2024-01-24 07:04:11 浏览: 211
好的,下面是一个Android Studio示例,可以实现Intent传递数组、Intent传递集合、Intent传递对象、Intent传递Bitmap。其中,我定义了一个Person类,用于演示Intent传递对象;另外,我在MainActivity中编写了四个方法,每个方法分别实现了一种Intent传递方式。
1. Intent传递数组:
```java
public void intentWithArray() {
String[] array = new String[]{"item1", "item2", "item3"};
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("array", array);
startActivity(intent);
}
```
在接收端的SecondActivity中,可以通过以下代码获取数组:
```java
String[] array = getIntent().getStringArrayExtra("array");
```
2. Intent传递集合:
```java
public void intentWithList() {
List<String> list = new ArrayList<>();
list.add("item1");
list.add("item2");
list.add("item3");
Intent intent = new Intent(this, SecondActivity.class);
intent.putStringArrayListExtra("list", (ArrayList<String>) list);
startActivity(intent);
}
```
在接收端的SecondActivity中,可以通过以下代码获取集合:
```java
List<String> list = getIntent().getStringArrayListExtra("list");
```
3. Intent传递对象:
```java
public void intentWithObject() {
Person person = new Person("Tom", 20);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("person", person);
startActivity(intent);
}
```
在接收端的SecondActivity中,可以通过以下代码获取对象:
```java
Person person = (Person) getIntent().getSerializableExtra("person");
```
4. Intent传递Bitmap:
```java
public void intentWithBitmap() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bitmap", bitmap);
startActivity(intent);
}
```
在接收端的SecondActivity中,可以通过以下代码获取Bitmap:
```java
Bitmap bitmap = getIntent().getParcelableExtra("bitmap");
```
完整的MainActivity代码如下:
```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnArray = findViewById(R.id.btn_array);
Button btnList = findViewById(R.id.btn_list);
Button btnObject = findViewById(R.id.btn_object);
Button btnBitmap = findViewById(R.id.btn_bitmap);
btnArray.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intentWithArray();
}
});
btnList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intentWithList();
}
});
btnObject.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intentWithObject();
}
});
btnBitmap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intentWithBitmap();
}
});
}
public void intentWithArray() {
String[] array = new String[]{"item1", "item2", "item3"};
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("array", array);
startActivity(intent);
}
public void intentWithList() {
List<String> list = new ArrayList<>();
list.add("item1");
list.add("item2");
list.add("item3");
Intent intent = new Intent(this, SecondActivity.class);
intent.putStringArrayListExtra("list", (ArrayList<String>) list);
startActivity(intent);
}
public void intentWithObject() {
Person person = new Person("Tom", 20);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("person", person);
startActivity(intent);
}
public void intentWithBitmap() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bitmap", bitmap);
startActivity(intent);
}
}
```
另外,Person类的代码如下:
```java
public class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
```
希望这个示例能够帮助你理解Intent传递数组、集合、对象和Bitmap的实现方式。
阅读全文