效果:
点击下图的“下一个”按钮实现人物信息切换
注意的步骤:
创建一个对象集合
代码:
ability_main.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:alignment="center"
ohos:orientation="vertical">
<Image
ohos:id="$+id:img"
ohos:height="match_content"
ohos:width="match_content"
ohos:image_src="$media:girl1"
/>
<Text
ohos:id="$+id:name"
ohos:height="50vp"
ohos:width="150vp"
ohos:text="姓名:王美花"
ohos:text_size="20fp"
/>
<Text
ohos:id="$+id:age"
ohos:height="50vp"
ohos:width="150vp"
ohos:text="年龄:29"
ohos:text_size="20fp"
/>
<Text
ohos:id="$+id:address"
ohos:height="50vp"
ohos:width="150vp"
ohos:text="地址:南京"
ohos:text_size="20fp"
/>
<Button
ohos:id="$+id:next"
ohos:height="50vp"
ohos:width="150vp"
ohos:background_element="#92D050"
ohos:text="下一个"
ohos:text_size="20fp"
ohos:text_color="#FFFFFF"
/>
<Button
ohos:id="$+id:get"
ohos:height="50vp"
ohos:width="150vp"
ohos:background_element="#92D050"
ohos:text="获取联系方式"
ohos:text_size="20fp"
ohos:text_color="#FFFFFF"
/>
</DirectionalLayout>
GirlFriend.java
package com.example.myapplication.domain;
import java.util.ConcurrentModificationException;
public class GirlFriend {
//照片
private int photoID;
//姓名
private String name;
//年龄
private int age;
//地址
private String address;
//空参 + 全参
//alt + insert快捷键
//或alt + Fn +insert快捷键
public GirlFriend(){
}
public GirlFriend(int photoID, String name, int age, String address) {
this.photoID = photoID;
this.name = name;
this.age = age;
this.address = address;
}
//get set
public int getPhotoID() {
return photoID;
}
public void setPhotoID(int photoID) {
this.photoID = photoID;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
MainAbilitySlice.java
package com.example.myapplication.slice;
import com.example.myapplication.ResourceTable;
import com.example.myapplication.domain.GirlFriend;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import java.util.ArrayList;
import java.util.Random;
public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
Image img;
Text name;
Text age;
Text address;
Button next;
Button get;
ArrayList<GirlFriend> list;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
//1.找到组件对象
img = (Image) findComponentById(ResourceTable.Id_img);
name = (Text) findComponentById(ResourceTable.Id_name);
age = (Text) findComponentById(ResourceTable.Id_age);
address = (Text) findComponentById(ResourceTable.Id_address);
next = (Button) findComponentById(ResourceTable.Id_next);
get = (Button) findComponentById(ResourceTable.Id_get);
//2. 创建一个集合装对象
list = new ArrayList<>();
//添加对象
//跟服务器交互之后,这些数据都是在服务器中获取的
//CTRL + p 快速查看类型
list.add(new GirlFriend(ResourceTable.Media_girl1, "王美花1", 29, "南京"));
list.add(new GirlFriend(ResourceTable.Media_girl2, "王美花2", 30, "天津"));
list.add(new GirlFriend(ResourceTable.Media_girl3, "王美花3", 31, "河北"));
list.add(new GirlFriend(ResourceTable.Media_girl4, "王美花4", 35, "内蒙古"));
list.add(new GirlFriend(ResourceTable.Media_girl5, "王美花5", 21, "河南"));
list.add(new GirlFriend(ResourceTable.Media_girl6, "王美花6", 22, "北京"));
list.add(new GirlFriend(ResourceTable.Media_girl7, "王美花7", 23, "深圳"));
list.add(new GirlFriend(ResourceTable.Media_girl8, "王美花8", 24, "江南"));
list.add(new GirlFriend(ResourceTable.Media_girl9, "王美花9", 25, "海南"));
//3. 给按钮添加点击事件
next.setClickedListener(this);
get.setClickedListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
Random r = new Random();
@Override
public void onClick(Component component) {
if (component == next) {
//换信息
//从集合中获取一个一个随机信息
//huo获取一个随机索引
int randomIdex = r.nextInt(list.size());
//通过随机索引获取信息
GirlFriend gf = list.get(randomIdex);
//把随机出来的信息设置到界面当中就可以了
img.setImageAndDecodeBounds(gf.getPhotoID());
name.setText("姓名" + gf.getName());
age.setText("年龄" + gf.getAge());
address.setText("地址" + gf.getAddress());
}else if(component == get){
//获取联系方式
}
}
}