Index.ets 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/**
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import router from '@system.router';
16
import promptAction from '@ohos.promptAction';
17 18 19 20 21

@Entry
@Component
struct ScrollExample {
  scroller: Scroller = new Scroller()
22 23
  private arr: number[] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
  @State hoverText : string = 'jump'
24 25 26 27 28 29 30 31 32

  build() {
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
      Text('MainPage')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
      Stack({ alignContent: Alignment.TopStart }) {
        Scroll(this.scroller) {
          Column() {
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
            Button('toast').fontSize(25).fontWeight(FontWeight.Bold)
              .onClick(() => {
                try {
                  promptAction.showToast({
                    message: 'toastShow',
                    duration: 60000
                  });
                } catch (error) {
                  console.error(`showToast args error code is ${error.code}, message is ${error.message}`);
                };
              })
            Button('dialog').fontSize(25).fontWeight(FontWeight.Bold)
              .onClick(() => {
                AlertDialog.show(
                  {
                    title: '',
                    message: 'dialogShow',
                    alignment: DialogAlignment.Bottom,
                    confirm: {
                      value: 'close',
                      action: () => {
                        console.info('close dialog')
                      }
                    }
                  }
                )
              })
60 61 62 63 64 65 66 67 68 69 70 71
            Button() {
              Text('next page')
                .fontSize(25)
                .fontWeight(FontWeight.Bold)
            }
            .key('my-key')
            .type(ButtonType.Capsule)
            .margin({ top: 20 })
            .onClick(() => {
              router.push({ uri: 'pages/second' })
            })
            .gesture(
72 73 74 75
              LongPressGesture({ repeat: false })
                .onAction((event: GestureEvent) => {
                  router.push({ uri: 'pages/fourth' })
                })
76 77 78 79 80 81 82 83 84
            )
            Button() {
              Text('Click twice')
                .fontSize(25)
                .fontWeight(FontWeight.Bold)
            }
            .type(ButtonType.Capsule)
            .margin({ top: 20 })
            .gesture(
85 86 87 88
              TapGesture({ count: 1 })
                .onAction(() => {
                  router.push({ uri: 'pages/third' })
                })
89 90
            )
            Button() {
91
              Text(this.hoverText)
92 93 94 95 96
                .fontSize(25)
                .fontWeight(FontWeight.Bold)
            }
            .type(ButtonType.Capsule)
            .margin({ top: 20 })
97 98 99 100
            .onHover((isHover: boolean) => {
              if (isHover) {
                this.hoverText = 'hover'
              }
101
            })
102 103 104 105 106 107 108 109 110 111 112 113 114
            .onMouse((event: MouseEvent) => {
              switch (event.button) {
                case MouseButton.Left :
                  this.hoverText = 'left'
                  break
                case MouseButton.Right :
                  this.hoverText = 'right'
                  break
                case MouseButton.Middle :
                  this.hoverText = 'middle'
                  break
              }
            })
115
            .onClick(() => {
Q
qpzeng 已提交
116
              router.push({ uri: 'pages/pinch' })
117
            })
118
            .gesture(
119 120 121 122
              LongPressGesture({ repeat: false })
                .onAction((event: GestureEvent) => {
                  router.push({ uri: 'pages/drag' })
                })
123
            )
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
            Checkbox({ name: 'hi' })
              .size({ width: 30, height: 30 })
            TextInput({ placeholder: 'welcome', text: 'Hello World' })
              .type(InputType.Normal)
              .width(300)
              .height(50)
              .fontSize(40)
              .enabled(true)
              .margin({ top: 20 })
            ForEach(this.arr, (item) => {
              Text(item.toString())
                .width('100%')
                .height('30%')
                .backgroundColor(0xFFFFFF)
                .borderRadius(75)
                .fontSize(80)
                .textAlign(TextAlign.Center)
                .margin({ top: 10 })
            }, item => item)
            Button() {
              Text('bottom')
                .fontSize(25)
                .fontWeight(FontWeight.Bold)
            }.type(ButtonType.Capsule)
            .margin({
              top: 20, left: 150
            })
            .onClick(() => {
              router.push({ uri: 'pages/second' })
            })
          }.width('100%')
        }
        .scrollable(ScrollDirection.Vertical)
        .scrollBar(BarState.On)
        .scrollBarColor(Color.Gray)
        .scrollBarWidth(30)
        .onScroll((xOffset: number, yOffset: number) => {
          console.info(xOffset + ' ' + yOffset)
        })
      }.width('100%').height('100%').backgroundColor(0xDCDCDC)
164 165 166
      Text('MainPage_End')
        .fontSize(50)
        .fontWeight(FontWeight.Bold)
167 168 169 170
    }
  }
}