HarmonyOS开发17:案例——相亲APP

实现功能:在app界面点击 “下一个” 按钮,可以更换小姐姐的信息
实现步骤:
1.首先将所需要的使用到的图片放入media文件夹

2.修改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"
        ohos:top_margin="10vp"
        />

</DirectionalLayout>

2.新建一个domain包,存放GirdFriend.java

在这里插入图片描述
3.GirdFriend.java代码

package com.example.imageapplication.domain;

public class GirlFriend {
    //照片
    private int photoID;
    //姓名
    private String name;
    //年龄
    private int age;
    //地址
    private String address;

    //空参 + 全参
    //alt +insert
    public GirlFriend(int media_girl1, String 王梅花, Text age, String 南京) {
    }

    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;
    }
}

4.MainAbilitySlice.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<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.创建一个集合,装9个女朋友对象
        list = new ArrayList<>();
        list.add(new GirlFriend(ResourceTable.Media_girl1, "王梅花1", 29, "南京"));
        list.add(new GirlFriend(ResourceTable.Media_girl2, "王梅花2", 21, "上海"));
        list.add(new GirlFriend(ResourceTable.Media_girl3, "王梅花3", 28, "北京"));
        list.add(new GirlFriend(ResourceTable.Media_girl4, "王梅花4", 26, "天津"));
        list.add(new GirlFriend(ResourceTable.Media_girl5, "王梅花5", 19, "河北"));
        list.add(new GirlFriend(ResourceTable.Media_girl6, "王梅花6", 21, "湖南"));
        list.add(new GirlFriend(ResourceTable.Media_girl7, "王梅花7", 25, "四川"));
        list.add(new GirlFriend(ResourceTable.Media_girl8, "王梅花8", 24, "晋江"));
        list.add(new GirlFriend(ResourceTable.Media_girl9, "王梅花9", 30, "长春"));

        //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){
            //点击的是获取联系方式
            //等以后学习了后面的知识,就可以跳转界面让用户充值
            //充值成功之后再获取小姐姐的联系方式
        }
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GeniusAng丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值