当前位置: 首页 > 后端技术 > Java

HarmonyOS实战——实现相亲APP

时间:2023-04-01 15:48:56 Java

简易版相亲APP实现如下效果:喜欢就点下方“联系方式”,不喜欢就点“下一步”将其复制到媒体文件夹。需要以上图片素材的朋友可以自取:https://www.aliyundrive.com/s...当然你也可以在网上找。图片信息如下:在上面的实现中可以看到布局实现方式:一张图片+三个文字+两个按钮:ability_main<文本ohos:id="$+id:name"ohos:height="50vp"ohos:width="150vp"ohos:text="姓名:王美华"ohos:text_size="20fp"/><按钮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"/>运行:2.核心业务逻辑实现首先找到组件对象业务:点击按钮“Next”修改以上信息,即可Can'看不到上述信息的整体?现在有9个相亲对象,点击“下一步”后,会在最上面随机显示一个相亲对象的信息创建一个Javabean类来描述女朋友信息。只有有了Javabean类,才能在代码中创建对象。在域中创建一个新类名:GirlFriend。类中有哪些属性?比如:图片、姓名、地址、年龄都是GirlFriend类中的属性。在资源管理器中获取图片,发现女孩图片是int类型的,所以在JavaBean中的GirlFriend类中,图片类型使用int类型。其他属性使用相应的类型。最后生成标准JavaBean包com.xdr630.makefriendsapplication.domain;publicclassGirlFriend{//照片privateintphotoID;//名称私有字符串名称;//年龄私人年龄;//地址私有字符串地址;//空参考+完整参考:alt+insertpublicGirlFriend(){}publicGirlFriend(intphotoID,Stringname,intage,Stringaddress){this.photoID=photoID;this.name=名称;这个。年龄=年龄;这。地址=地址;}publicintgetPhotoID(){返回photoID;}publicvoidsetPhotoID(intphotoID){this.photoID=photoID;}publicStringgetName(){返回名称;}publicvoidsetName(Stringname){这个。名字=名字;}publicintgetAge(){返回年龄;}民众voidsetAge(intage){this.age=age;}publicStringgetAddress(){返回地址;}publicvoidsetAddress(Stringaddress){this.address=address;}}Javabean创建完成后,可以创建一个集合安装9个相亲对象,使用集合存储,ArrayList,泛型可以直接写创建的GirlFriend类。以后学习和服务器交互后,这些数据都是从服务器获取的,就不一一添加了。下面是手动添加数据源创建一个女朋友对象(newGirlFriend)并添加到列表中。为方便起见,省去检查Javabeans中属性的排序,Ctrl+P查看参数。添加9个对象,然后添加按钮在点击事件中,必须添加两个按钮,分别是“下一步”和“获取”按钮,在该类中实现点击事件。当点击下一个按钮时,会执行该类中的onClick方法。get按钮同上,click事件在onClick方法中判断是next按钮还是get按钮。因为在onClick方法中使用了按钮的对象,所以只有将按钮的对象提升为成员变量,才能在onClick方法中访问到按钮的对象。其中,这些组件都是用到的,所以都是作为成员变量提到的。在下面的onClick方法中,女孩信息必须从集合中获取,所以集合也应该作为成员变量被提及。Random对象也放在member位置,表示程序启动后就创建一个,如果放在onClick方法中,点击时会创建一次,内存冗余。MainAbilitySlicepackagecom.xdr630.makefriendsapplication.slice;导入com.xdr630.makefriendsapplication.ResourceTable;导入com.xdr630.makefriendsapplication.domain.GirlFriend;导入ohos.aafwk.ability.AbilitySlice;导入ohos.aafwk.content.Intent;导入ohos。agp.components.Button;导入ohos.agp.components.Component;导入ohos.agp.components.Image;导入ohos.agp.components.Text;导入java.util.ArrayList;导入java.util.Random;公共类MainAbilitySlice扩展AbilitySlice实现Component.ClickedListener{Imageimg;文字名称;文字时代;文字地址;下一个按钮;按钮得到;ArrayListlist=newArrayList<>();@OverridepublicvoidonStart(Intentintent){super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);//1.找到组件对像img=(Image)findComponentById(ResourceTable.Id_img);名称=(文本)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(newGirlFriend(ResourceTable.Media_girl1,"王梅花1",29,"南京”));list.add(newGirlFriend(ResourceTable.Media_girl2,"王梅花2",30,"上海"));list.add(newGirlFriend(ResourceTable.Media_girl3,"王梅花3",31,"武汉"));list.add(newGirlFriend(ResourceTable.Media_girl4,"王梅花4",28,"长沙"));list.add(newGirlFriend(ResourceTable.Media_girl5,"王梅花5",25,"南昌"));list.add(newGirlFriend(ResourceTable.Media_girl6,"王梅花6",26,"杭州"));list.add(newGirlFriend(ResourceTable.Media_girl7,"王梅花7",23,"深圳"));list.add(newGirlFriend(ResourceTable.Media_girl8,"王美花8",22,"北京"));list.add(newGirlFriend(ResourceTable.Media_girl9,"王梅花9",24,"广州"));//3.给按钮添加点击事件next.setClickedListener(this);get.setClickedListener(this);}@OverridepublicvoidonActive(){super.onActive();}@OverridepublicvoidonForeground(Intentintent){super.onForeground(intent);}Randomr=newRandom();@OverridepublicvoidonClick(Componentcomponent){if(component==next){//点击下一个---更改一个女孩的信息//从集合中随机获取一个女孩的信息//获取一个randomindexintrandomIndex=r.nextInt(list.size());//通过随机索引获取随机女孩信息GirlFriendgirlFriend=list.get(randomIndex);//将随机信息设置到接口img.setImageAndDecodeBounds(girlFriend.getPhotoID());name.setText("姓名:"+girlFriend.getName());age.setText("年龄:"+girlFriend.getAge());address.setText("地址:"+girlFriend.getA地址());}elseif(component==get){//获取联系方式//拓展:可以跳转到用户充值界面,充值后可以看到妹子信息}}}运行:点击“下一步”时点击“下一步”按钮,信息也会一并获取,有需要的朋友可以展开。单击“下一步”按钮时,信息被隐藏。只有当点击“获取联系方式”按钮时才会显示相应的信息,或者可以添加充值页面,点击“获取联系方式”时会跳转到充值页面,只能看到相应的充值页面充值后信息