HarmonyOS--从简单页面跳转开始3

熟悉基本的布局简单使用Tabs、list、grid做一个类似微信登录的页面
在这里插入图片描述
图片素材链接

先准备登录页面和登录跳转页,主要router的使用
跳转页Tabs
在这里插入图片描述
Tabs的简单使用

@Entry
@Component
struct TabsExample {
  private controller: TabsController = new TabsController()

  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Green)
        }
        .tabBar('green')

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Blue)
        }
        .tabBar('blue')

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Yellow)
        }
        .tabBar('yellow')

        TabContent() {
          Column().width('100%').height('100%').backgroundColor(Color.Pink)
        }
        .tabBar('pink')
      }
      .barWidth('100%') // 设置TabBar宽度
      .barHeight(60) // 设置TabBar高度
      .width('100%') // 设置Tabs组件宽度
      .height('100%') // 设置Tabs组件高度
      .backgroundColor(0xF5F5F5) // 设置Tabs组件背景颜色
    }
    .width('100%')
    .height('100%')
  }
}

效果图:
在这里插入图片描述
至于List和Grid见上篇
代码目录
在这里插入图片描述
各代码如下:
ContactPerson.ets

export default class ContactPerson{
  img?: Resource;
  title?: string;
  others?: string;

  constructor(img?: Resource,title?: string,others?: string) {
    this.img = img;
    this.title = title;
    this.others = others;
  }
}

ItemData.ets

//1.定义列表数据对象
export default class ItemData{
  img?: Resource;
  title?: Resource;
  others?: Resource;

  constructor(img?: Resource,title?: Resource,others?: Resource) {
    this.img = img;
    this.title = title;
    this.others = others;
  }
}

home.ets

import App from '@system.app';
import TabMe from '../view/TabMe';
import router from '@ohos.router';
import TabDiscover from '../view/TabDiscover';
import TabBook from '../view/TabBook';
@Entry
@Component
struct Home {
  @State name: string = router.getParams()?.['name']
  @State password: string = router.getParams()?.['password']
  @State currentIndex: number = 0;
  private tabsController:TabsController = new TabsController();
  //单个tab布局
  @Builder
  TabBuilder(title: string,index: number,selectImg: Resource,normalImg: Resource){
    Column(){
      Image(this.currentIndex === index ? selectImg : normalImg)
        .width(30)
        .height(30)
      Text(title)
        .fontColor(this.currentIndex === index ? 0x00ff00 : 0x000000)
        .fontSize(20)
        .margin({
          top:10
        })
    }
    .width(70)
    .height(70)
    .justifyContent(FlexAlign.Center)
    .onClick((event : ClickEvent)=>{
        this.currentIndex = index;
        this.tabsController.changeIndex(this.currentIndex);
    })
  }

  //页签切换tabs
  build() {
    Tabs({
        barPosition: BarPosition.End,
        controller:this.tabsController
    }){
        TabContent(){
          Column(){
              Row(){
                Image($r('app.media.contact_person'))
                  .height(70)
                  .width(70)
                  .margin({
                    left:20
                  })
                Column(){
                  Text('晴天小霹雳')
                    .fontSize(25)
                    .height(40)
                  Text('我们互删吧')
                    .fontSize(20)
                    .fontColor(0xff0000)
                    .height(30)
                }.margin({
                  left:30
                })
              }.height(70)
              .width('100%')
          }.height('100%')

        }.tabBar(this.TabBuilder('微信',0,$r('app.media.tab_wechat_select_y'),$r('app.media.tab_wechat_select_n')))
        TabContent(){
          Column(){
            TabBook()
          }.height('100%')

        }.tabBar(this.TabBuilder('通讯录',1,$r('app.media.tab_book_select_y'),$r('app.media.tab_book_select_n')))
        TabContent(){
          TabDiscover()
        }.tabBar(this.TabBuilder('发现',2,$r('app.media.tab_discover_select_y'),$r('app.media.tab_discover_select_n')))
        TabContent(){
          Column(){
            Row(){
              Image($r('app.media.admin_img'))
                .height(100)
                .width(100)
                .margin({
                  left:20
                })
              Column(){
                Text(this.name)
                  .fontSize(20)
                  .height(65)
                Text('用户名:'+this.name)
                  .fontSize(15)
                  .height(35)
              }
            }.height(100)
              .width('100%')
              TabMe()
          }

        }.tabBar(this.TabBuilder('我',3,$r('app.media.tab_me_select_y'),$r('app.media.tab_me_select_n')))
    }
    .vertical(false)
    .barWidth('100%')
    .barHeight(70)
    .onChange((index: number)=>{
      this.currentIndex = index;
    })
  }
}

Login.ets

import Router from '@system.router'
import router from '@ohos.router'
@Entry
@Component
struct Login {
  @State name:string = '' //写成大写的会undefined
  @State password:string = ""
  build() {
    Column(){
      //图标居中
      Row(){
        Image($r('app.media.login_'))
          .height(80)
          .width(80)
      }.margin({
        top:120
      })
      .justifyContent(FlexAlign.Center)
      .width('100%')
      //文字居中
      Row(){
        Text('账号登录')
          .fontSize(30)
          .fontColor(0x000000)
      }
        // .alignItems(VerticalAlign.Center)
        .margin({
          top:40
        })
      .justifyContent(FlexAlign.Center)
      .width('100%')

      //账号输入框
      TextInput({
        placeholder:'账号:'
      }).placeholderColor(0x999999)
        .placeholderFont({
          size:25,
          weight:FontWeight.Medium,
          family: 'cursive',
          style:FontStyle.Italic //斜体
        })
        .margin({
          top:20
        })
        .onChange((name: string)=>{
          this.name = name;
        })
      //密码输入框
      TextInput({
        placeholder:'密码:'
      }).placeholderColor(0x999999)
        .placeholderFont({
          size:25,
          weight:FontWeight.Medium,
          family: 'cursive',
          style:FontStyle.Italic //斜体
        })
        .type(InputType.Password)
        .margin({
          top:20
        })
        .onChange((password: string)=>{
          this.password = password;
        })
      //文字
      Row(){
        Text('短信验证')
          .fontColor(0x0070ff)
          .fontSize(20)
        Text('忘记密码')
          .fontColor(0x0070ff)
          .fontSize(20)
      }.justifyContent(FlexAlign.SpaceAround)
      .width('100%')
      .margin({
        top:40
      })

      Button('登   录')
        .type(ButtonType.Capsule)//当开启按压态显示效果,开发者设置状态样式时,会基于状态样式设置完成后的背景色再进行颜色叠加。
        .stateEffect(true)
        .fontSize(20)
        .width('80%')
        .margin({
          top:50
        })
        .onClick(()=>{
          if(this.name === '123' && this.password ==='123'){
            router.pushUrl({
              url:'pages/home',
              params:{
                name : this.name,
                password : this.password
              }
            },router.RouterMode.Standard)

            console.log('登录成功'+this.name+this.password)
          }else {
            console.log('登录失败'+this.name+this.password)
          }
        })


    }
  }
}

TabBook.ets

import ContactPerson from '../common/bean/ContactPerson'
import ItemData from '../common/bean/ItemData'
import MainViewModel from '../viewmodel/MainViewModel'
@Component
export default struct TabBook{

  @Builder
  buildContactPerson(itemPerson: ContactPerson){
    Row(){
      Image(itemPerson.img)
        .height(50)
        .width(50)
        .margin({
          left:20
        })
      Text(itemPerson.title)
        .height(50)
        .width(150)
        .fontSize(18)
        .margin({
          left:30
        })
    }.height(50)
    .width('100%')
    .margin({
      left:10,
      top:10
    })
  }

  build(){
    Column(){
      //Grid
      Grid(){
        ForEach(MainViewModel.getTabBookGridData(),(item: ItemData)=>{
          GridItem(){
            Row(){
              Image(item.img)
                .height(50)
                .width(50)
                .objectFit(ImageFit.Fill)
                .margin({
                  left:10
                })
              Text(item.title)
                .height(50)
                .width(150)
                .fontSize(25)
                .margin({
                  left:30
                })
            }.width('100%')
            .justifyContent(FlexAlign.Start)
          }
        },item=>JSON.stringify(item))
      }
      .rowsTemplate('1fr 1fr')
      .columnsTemplate('1fr')
      .columnsGap(10)
      .height(110)
      .width('100%')
      .margin({
        top:20
      })
      //text
      Row(){
        Text('我的联系人')
          .fontSize(20)
          .fontColor(0x999999)
          .width('100%')
          .margin({
            left:20,
            top:50
          })
      }.justifyContent(FlexAlign.Start)

      //List
      Scroll(){
        Column(){
          List({space:12}){
            ForEach(MainViewModel.getTabBookListData(),(itemPerson: ContactPerson)=>{
              ListItem(){
                this.buildContactPerson(itemPerson);
              }
            },itemPerson=>JSON.stringify(itemPerson))
          }
        }
      }
      .height(450)
      .scrollable(ScrollDirection.Vertical)
      .margin({
        top:20
      })
      

    }
  }
}

TabMe.ets

import ItemData from '../common/bean/ItemData'
import MainViewModel from '../viewmodel/MainViewModel'
@Component
export  default struct TabMe {
  @Builder
  buildMe(item: ItemData){
      Row(){
        Image(item.img)
          .height(50)
          .width(50)
          .objectFit(ImageFit.Fill)
          .margin({
          left:20
        })
        Text(item.title)
          .height(50)
          .width(100)
          .fontSize(25)
          .margin({
            left:30
          })
        if(item.others === null){
          Image($r('app.media.me_more'))
            .height(30)
            .width(30)
            .margin({
              left:100
            })
        }
      }.width(50)
        .margin({
          top:20
        })
  }

  build(){
    Column(){
      List(){
        ForEach(MainViewModel.getTabMeListData(),(item: ItemData)=>{
          ListItem(){
            this.buildMe(item);
          }.height(50)
        },item=>JSON.stringify(item))

      }
    }.height('100%')
    .width('100%')
    .margin({
      top:50
    })

  }
}

TabDiscover.ets

import ItemData from '../common/bean/ItemData'
import MainViewModel from '../viewmodel/MainViewModel'
@Component
export default struct TabDiscover{
  @Builder
  buildDiscover(item: ItemData){
    Row(){
      Image(item.img)
        .height(50)
        .width(50)
        .objectFit(ImageFit.Fill)
        .margin({
          left:20
        })
      Text(item.title)
        .height(50)
        .width(100)
        .fontSize(25)
        .margin({
          left:30
        })
      if(item.others === null){
        Image($r('app.media.me_more'))
          .height(30)
          .width(30)
          .margin({
            left:100
          })
      }
    }.width(50)
    .margin({
      top:20
    })
  }
  build(){
    Column(){
      Row(){
        Text('发现')
          .fontSize(23)
          .fontStyle(FontStyle.Normal)
      }.justifyContent(FlexAlign.Center)
      List(){
        ForEach(MainViewModel.getTabDiscoverListData(),(item: ItemData)=>{
          ListItem(){
            this.buildDiscover(item);
          }.height(50)
        },item=>JSON.stringify(item))

      }
    }.height('100%')
    .width('100%')
    .margin({
      top:10
    })
  }
}

MainViewModel.ets

import ContactPerson from '../common/bean/ContactPerson';
import ItemData from '../common/bean/ItemData';
export  class MainViewModel{
  //数据数组列表
  getTabMeListData(): Array<ItemData>{
    let tabMeListData: ItemData[] = [
      new ItemData($r('app.media.me_server'),$r('app.string.me_server'),null),
      new ItemData($r('app.media.me_collect'),$r('app.string.me_collect'),null),
      new ItemData($r('app.media.me_circle'),$r('app.string.me_circle'),null),
      new ItemData($r('app.media.me_card'),$r('app.string.me_card'),null),
      new ItemData($r('app.media.me_emote'),$r('app.string.me_emote'),null),
      new ItemData($r('app.media.me_setting'),$r('app.string.me_setting'),null)
    ]
    return tabMeListData;
  }

  getTabDiscoverListData(): Array<ItemData>{
    let tabDiscoverListData: ItemData[] = [
      new ItemData($r('app.media.discover_circle'),$r('app.string.discover_circle'),null),
      new ItemData($r('app.media.discover_game'),$r('app.string.discover_game'),null),

    ]
    return tabDiscoverListData;
  }

  getTabBookGridData(): Array<ItemData>{
    let tabBookGridData: ItemData[] = [
      new ItemData($r('app.media.book_add'),$r('app.string.book_add'),null),
      new ItemData($r('app.media.book_group'),$r('app.string.book_group'),null)
    ]
    return tabBookGridData;
  }

  getTabBookListData(): Array<ContactPerson>{
    let tabBookListData: ContactPerson[] = [
      new ContactPerson($r('app.media.contact_person_1'),'联系人1',null),
      new ContactPerson($r('app.media.contact_person'),'联系人2',null),
      new ContactPerson($r('app.media.contact_person_2'),'联系人3',null),
      new ContactPerson($r('app.media.contact_person_1'),'联系人4',null),
      new ContactPerson($r('app.media.contact_person'),'联系人5',null),
      new ContactPerson($r('app.media.contact_person_2'),'联系人6',null),
      new ContactPerson($r('app.media.contact_person_1'),'联系人7',null),
      new ContactPerson($r('app.media.contact_person'),'联系人8',null),
      new ContactPerson($r('app.media.contact_person_2'),'联系人9',null),
      new ContactPerson($r('app.media.contact_person_1'),'联系人10',null),
      new ContactPerson($r('app.media.contact_person'),'联系人11',null),
      new ContactPerson($r('app.media.contact_person_2'),'联系人12',null)
    ]
    return tabBookListData;
  }
}
export default new MainViewModel();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值