简易版 相亲APP

  • 实现如下成果:
  • 如果喜爱就点击上面的“联系方式”,不喜爱就点击“下一个

1. 布局实现

  • 新建我的项目:MakeFriendsApplication
  • 把以下九个girl图片复制到 media 文件夹下

  • 有须要下面图片素材的小伙伴能够自取:https://www.aliyundrive.com/s...
  • 当然也能够本人在网上找,图片信息如下:

  • 能够在下面的实现看到布局是由:一个图片+三个文本+两个按钮 组成的
  • 布局实现:

ability_main

<?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. 外围业务逻辑实现

  • 先找到组件对象

  • 业务:点击了按钮“下一个”就要批改下面的信息,能不能把下面的信息看作成一个整体的对象?
    当初有9个相亲对象,点击“下一个”的时候,就会随机把一个相亲对象的信息去显示在下面。
  • 创立一个Javabean类去形容女朋友信息,有了Javabean类后能力在代码中创建对象

  • domain中新建类名为:GirlFriend,在类中的属性有哪些?
  • 如:图片、姓名、地址、年龄都是GirlFriend类中的属性
  • 在资源管理器中获取图片,发现girl图片是int类型的

  • 所以在JavaBean中GirlFriend类中图片类型应用int类型

  • 其余属性应用对应的类型

  • 最初生成规范的JavaBean即可
package com.xdr630.makefriendsapplication.domain;public class GirlFriend {    //照片    private int photoID;    //姓名    private String name;    //年龄    private int age;    //地址    private String address;    //空参 + 全参:alt + 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;    }    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;    }}
  • 创立好了Javabean,就能够创立一个汇合装9个相亲的对象,应用汇合来存储了,ArrayList,泛型就能够间接写创立好的GirlFriend

  • 在当前学习了和服务器交互之后,这些数据都是从服务器获取的,就不是一个一个增加了
  • 上面就是手动增加数据起源
  • 创立一个女朋友对象(new GirlFriend )并增加到 list 当中
  • 为了不便,省的去查看Javabean中属性的排序,Ctrl + P 就能够查看参数了

  • 增加实现9个对象

  • 接着给按钮增加点击事件,两个按钮都要增加,别离是“next”和“get”按钮
  • 实现本类中点击事件

  • next按钮被点击后,就会执行本类中的onClick办法

  • get 按钮同上也要增加单击事件

  • 而后在 onClick 办法中判断点击是 next 按钮 或 get 按钮
  • 因为在 onClick 办法中要用到按钮的对象,所以把按钮的对象晋升为 成员变量 能力在onClick办法中被拜访的到,用到哪个就提哪个,这些组件都用的到,所以都提为成员变量
  • 在上面onClick办法当中,要从汇合中获取girl信息,所以汇合也要提为成员变量

  • Random对象也放在成员地位,示意在程序启动之后就创立了一个,如果放在onClick办法中,当点击一次就创立一次,内存冗余。

MainAbilitySlice

package com.xdr630.makefriendsapplication.slice;import com.xdr630.makefriendsapplication.ResourceTable;import com.xdr630.makefriendsapplication.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 = 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",28,"长沙"));        list.add(new GirlFriend(ResourceTable.Media_girl5,"王美花5",25,"南昌"));        list.add(new GirlFriend(ResourceTable.Media_girl6,"王美花6",26,"杭州"));        list.add(new GirlFriend(ResourceTable.Media_girl7,"王美花7",23,"深圳"));        list.add(new GirlFriend(ResourceTable.Media_girl8,"王美花8",22,"北京"));        list.add(new GirlFriend(ResourceTable.Media_girl9,"王美花9",24,"广州"));        //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){            //点击的是下一个 --- 换一个girl的信息            //从汇合中获取一个随机的girl的信息            //获取一个随机索引            int randomIndex = r.nextInt(list.size());            //通过随机索引获取随机girl信息            GirlFriend girlFriend = list.get(randomIndex);            //把随机进去的信息设置到界面当中            img.setImageAndDecodeBounds(girlFriend.getPhotoID());            name.setText("姓名:" + girlFriend.getName());            age.setText("年龄:" + girlFriend.getAge());            address.setText("地址:" + girlFriend.getAddress());        }else if (component == get){            //获取联系方式            //扩大:能够跳转到界面让用户充值界面,充值后能力看到girl信息        }    }}
  • 运行:

  • 点击“下一个”

  • 当点击“下一个”按钮时,信息也会随之一起获取的到,所以有需要的小伙伴能够扩大下,当点击“下一个”按钮时信息是暗藏的,只有当点击“获取联系方式”时能力显示出对应的信息,或也能够增加充值页面,当点击“获取联系方式”时,就跳转到充值页面,只有充值完后能力看到对应的信息。