更多内容请访问:https://harmonyos.51cto.com,与华为官网共建的鸿蒙技术社区如果不喜欢“联系方式””,不喜欢就点“下一步”。1.布局实现一个新项目:MakeFriendsApplication复制下面九张女孩图片到media文件夹。需要以上图片素材的朋友可以自取:https://www.aliyundrive.com/s/j59dy5redPR当然你也可以自己上网找。图片信息如下:在上面的实现中可以看到布局是由一张图片+三个文字+两个按钮组成的:运行:2.核心业务逻辑实现首先找到组件对象业务:点击“下一步”按钮修改以上信息,能不能把以上信息看成一个整体对象?现在有9个相亲对象,点击“下一步”时,会在最上面随机显示一个相亲对象的信息创建一个Javabean类来描述女朋友信息。只有有了Javabean类,才能在代码中创建对象。在域中创建一个新类名:GirlFriend。类中有哪些属性?例如:图片,姓名,地址,年龄都是GirlFriend类中的属性在资源管理器中获取图片,发现女孩图片是int类型,所以在JavaBean中的GirlFriend类中,使用int类型为图片类型,其他属性使用对应的类型,最终生成一个标准的JavaBean。packagecom.xdr630.makefriendsapplication.domain;publicclassGirlFriend{//photoprivateintphotoID;//nameprivateStringname;//ageprivateintage;//addressprivateStringaddress;//空参数+全参数:alt+insertpublicGirlFriend(){}publicGirlFriend(intphotoID,Stringname,intage,Stringaddress){this.photoID=photoID;this.name=name;this.age=age;this.address=address;}publicintgetPhotoID(){returnphotoID;}publicvoidsetPhotoID(intphotoID){this.photoID=photoID;}publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}publicintgetAge(){returnage;}publicvoidsetAge(intage){this.age=age;}publicStringgetAddress(){returnaddress;}publicvoidsetAddress(Stringaddress){this.address=address;}}创建好Javabean后,就可以创建一个9个相亲对象的集合,使用集合来存储,ArrayList,泛型,可以直接写创建的GirlFriend类。后面学习了跟服务器交互之后,这些数据All从服务器获取,就不一一添加了。下面是手动添加数据源创建一个女朋友对象(newGirlFriend)并添加到列表中。为了方便,省去了Javabeans中属性排序的检查,Ctrl+P可以检查参数,添加9个对象,然后给按钮添加点击事件。必须添加两个按钮,即“下一步”和“获取”按钮,以在此类中实现点击事件。单击下一步按钮时,将执行此类。获取按钮中的onClick方法同上,同样添加了点击事件,然后在onClick方法中判断点击的是下一步按钮还是获取按钮。因为在onClick方法中使用了按钮的对象,所以将按钮的对象提升为一个成员变量,在onClick方法中访问,用哪个,哪个用哪个。这些组件都是用到的,所以都是作为成员变量提到的。在下面的onClick方法中,需要从集合中获取girl信息,所以集合也要作为成员变量提到Random对象也放在成员位置,也就是说程序启动后创建一个。如果放在onClick方法中,点击时会创建一次,内存冗余。MainAbilitySlicepackagecom.xdr630.makefriendsapplication.slice;importcom.xdr630.makefriendsapplication.ResourceTable;importcom.xdr630.makefriendsapplication.domain.GirlFriend;importohos.aafwk.ability.AbilitySlice;importohos.aafwk.content.Intent;importohos.agp.components.Button;importohos.agp.components.Component;importohos.agp.components.Image;importohos.agp.components.Text;importjava.util.ArrayList;importjava.util.Random;publicclassMainAbilitySliceextendsAbilitySliceimplementsComponent.ClickedListener{Imageimg;Textname;Textage;Textaddress;Buttonnext;Buttonget;ArrayListlist=newArrayList<>();@OverridepublicvoidonStart(Intentintent){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个对象//以后学习学习服务器交互后,从服务器列表中获取这些数据。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(组件组件){if(component==next){//点击下一个---更改一个女孩的信息//从集合中获取一个随机女孩的信息//获取一个随机索引intrandomIndex=r.nextInt(list.size());//通过随机索引获取随机女孩信息GirlFriendgirlFriend=list.get(randomIndex);//将随机信息设置到接口img.setImageAndDecodeBounds(girlFriend.getPhotoID());name.setText("Name:"+girlFriend.getName());age.setText("年龄:"+girlFriend.getAge());address.setText("地址:"+girlFriend.getAddress());}elseif(component==get){//获取联系方式//扩展:可以跳转到界面让用户充值界面,充值后可以看到妹子信息}}}操作:点“下一步”当点“Ne”xt”按钮,信息也会和它一起获取,有需要的可以展开。单击“下一步”按钮时,信息被隐藏。只有点击“获取联系方式”,才能显示相应的信息,也可以在充值页面添加,点击“获取联系方式”后,会跳转到充值页面,可以看到充值完成后才会显示相应信息更多信息请访问:Harmonyos.51cto.com,与华为官方合作打造的鸿蒙技术社区