> 文档中心 > 2021-08-17 HarmonyOS实战 Text和Image的使用

2021-08-17 HarmonyOS实战 Text和Image的使用

1.xml前端页面

<?xml version="1.0" encoding="UTF-8"?><DirectionalLayout ohos:orientation="vertical" ohos:alignment="center" ohos:width="match_parent" ohos:height="match_parent" xmlns:ohos="http://schemas.huawei.com/res/ohos"><Image ohos:width="match_content" ohos:height="match_content" ohos:image_src="$media:girl1" ohos:id="$+id:img"/><Text ohos:width="150vp" ohos:height="50vp" ohos:id="$+id:name" ohos:text_size="20fp" ohos:text="姓名:王美花"/><Text ohos:width="150vp" ohos:height="50vp" ohos:id="$+id:age" ohos:text_size="20fp" ohos:text="年龄:29"/><Text ohos:width="150vp" ohos:height="50vp" ohos:id="$+id:address" ohos:text_size="20fp" ohos:text="地址:南京"/><Button ohos:width="150vp" ohos:height="50vp" ohos:id="$+id:next" ohos:text_size="20fp" ohos:text="下一个" ohos:text_color="#FFFFFF" ohos:background_element="#92D050"/><Button ohos:width="150vp" ohos:height="50vp" ohos:id="$+id:get" ohos:text_size="20fp" ohos:text="获取联系方式" ohos:text_color="#FFFFFF" ohos:background_element="#92D050" ohos:top_margin="10vp"/>

2.相关类

package com.example.imageapplication.domain;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;    }}

3.后端处理java脚本

package com.example.imageapplication.slice;import com.example.imageapplication.ResourceTable;import com.example.imageapplication.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 list = new ArrayList();    @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.创建一个集合装9个女朋友对象 //添加9个对象 //在以后当我们学习了跟服务器交互之后,这些数据都是从服务器获取的。 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", 20, "南京")); list.add(new GirlFriend(ResourceTable.Media_girl7, "王美花7", 35, "南京")); list.add(new GirlFriend(ResourceTable.Media_girl8, "王美花8", 25, "南京")); list.add(new GirlFriend(ResourceTable.Media_girl9, "王美花9", 23, "南京")); //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) {     //点击的是下一个 --- 换一个妹子的信息     //从集合中获取一个随机的妹子信息     //获取一个随机索引、     int randomIndex = r.nextInt(list.size());     //通过随机索引获取随机的小姐姐信息     GirlFriend gf = list.get(randomIndex);     //把随机出来的信息设置到界面当中     img.setImageAndDecodeBounds(gf.getPhotoID());     name.setText("姓名:" + gf.getName());     age.setText("年龄:" + gf.getAge());     address.setText("地址:" + gf.getAddress()); } else if (component == get) {     //点击的是获取联系方式     //等以后学习了后面的知识,就可以跳转界面让用户充值     //充值成功之后再获取小姐姐的联系方式 }    }}

冰雪之城