
回复
javascriptimport { router } from '@kit.ArkUI';
import { httpRequestGet } from '../Utils/HttpUtils';
import { LoginModel, LoginModelUser } from '../model/LoginModel';
import promptAction from '@ohos.promptAction';
import { common } from '@kit.AbilityKit';
import { PositionList } from '../pages/PositionList';
import { MessageList } from '../pages/MessageList';
import { CompanyList } from '../pages/CompanyList';
import My from '../pages/My';
css@Entry
@Component
struct Home {
private backtime: number = 0;
@State message: string = 'Hello World';
@State tabindex: number = 0;
private controller: TabsController = new TabsController();
@State tablist: TableInterface[] = [
{
id: 0,
title: "职位",
icon: $r('app.media.ic_main_tab_company_pre'),
selecticon: $r('app.media.ic_main_tab_company_nor')
},
{
id: 1,
title: "公司",
icon: $r('app.media.ic_main_tab_find_pre'),
selecticon: $r('app.media.ic_main_tab_find_nor')
},
{
id: 2,
title: "消息",
icon: $r('app.media.ic_main_tab_contacts_pre'),
selecticon: $r('app.media.ic_main_tab_contacts_nor')
},
{
id: 3,
title: "我的",
icon: $r('app.media.ic_main_tab_my_pre'),
selecticon: $r('app.media.ic_main_tab_my_nor')
}
];
Home
的组件。@State
装饰器定义了tabindex
,用于存储当前选中的标签索引。controller
,用于控制标签的切换。tablist
,用于存储底部导航栏的标签信息。scss@Builder
tabBarItem(item: TableInterface) {
Column() {
Image(item.id === this.tabindex ? item.icon : item.selecticon)
.width(25)
.height(25)
.margin({ top: 5 })
Text(item.title)
.fontSize(20)
.fontColor(item.id === this.tabindex ? Color.Green : Color.Black)
.margin({ top: 5 })
.height(60)
.width("100%")
}
tabBarItem
方法,用于构建每个标签项的UI。tabindex
,动态切换图标和文字颜色。scssbuild() {
Row() {
Tabs({ index: $$this.tabindex, controller: this.controller }) {
ForEach(this.tablist, (item: TableInterface, index: number) => {
TabContent() {
if (0 === index) {
PositionList();
} else if (1 === index) {
CompanyList();
} else if (2 === index) {
MessageList();
} else if (3 === index) {
My();
}
.tabBar(this.tabBarItem(item))
})
.barPosition(BarPosition.End)
.scrollable(false) // 去掉滑动效果
.onChange((index: number) => {
.tabindex
.controller.changeIndex(this.tabindex);
})
.alignItems(VerticalAlign.Center)
.height("100%")
}
Row
和Tabs
布局组件,构建主页的UI。ForEach
遍历tablist
,为每个标签项生成对应的TabContent
。tabindex
,动态切换显示的功能页面。onChange
事件监听标签切换,更新tabindex
和controller
的索引。kotlinonBackPress(): boolean | void {
let nowtime = Date.now();
if (nowtime - this.backtime < 1000) {
const mContext = getContext(this) as common.UIAbilityContext;
mContext.terminateSelf();
} else {
this.backtime = nowtime;
promptAction.showToast({
message: "再按一次退出应用"
});
}
return true;
}
onBackPress
方法,用于处理返回键事件。通过上述代码,我们实现了底部导航栏的标签切换和对应的功能页面展示。用户可以通过点击底部导航栏的标签,切换到不同的功能页面。此外,还实现了返回键的防抖处理,提升了用户体验。