HarmonyOS 鸿蒙Next中slider怎么让已滑动部分在中间开始,并且可以左右滑动?

发布于 1周前 作者 nodeper 来自 鸿蒙OS

HarmonyOS 鸿蒙Next中slider怎么让已滑动部分在中间开始,并且可以左右滑动?

cke_589.jpeg


更多关于HarmonyOS 鸿蒙Next中slider怎么让已滑动部分在中间开始,并且可以左右滑动?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

6 回复

@Entry @Component struct SliderExample { @State outSetValueOne: number = 40 @State inSetValueOne: number = 40 @State outSetValueTwo: number = 40 @State inSetValueTwo: number = 40 @State vOutSetValueOne: number = 40 @State vInSetValueOne: number = 40 @State vOutSetValueTwo: number = 40 @State vInSetValueTwo: number = 40

build() { Column({ space: 8 }) { Text(‘outset slider’).fontSize(9).fontColor(0xCCCCCC).width(‘90%’).margin(15) Row() { Slider({ value: this.outSetValueOne, min: 0, max: 100, style: SliderStyle.OutSet }) .showTips(true) .onChange((value: number, mode: SliderChangeMode) => { this.outSetValueOne = value console.info(‘value:’ + value + ‘mode:’ + mode.toString()) }) // toFixed(0)将滑动条返回值处理为整数精度 Text(this.outSetValueOne.toFixed(0)).fontSize(12) } .width(‘80%’)

  Row() {
    Slider({
      value: this.outSetValueTwo,
      step: 10,
      style: SliderStyle.OutSet
    })
      .showSteps(true)
      .onChange((value: number, mode: SliderChangeMode) => {
        this.outSetValueTwo = value
        console.info('value:' + value + 'mode:' + mode.toString())
      })
    Text(this.outSetValueTwo.toFixed(0)).fontSize(12)
  }
  .width('80%')

  Text('inset slider').fontSize(9).fontColor(0xCCCCCC).width('90%').margin(15)
  Row() {
    Slider({
      value: this.inSetValueOne,
      min: 0,
      max: 100,
      style: SliderStyle.InSet
    })
      .blockColor('#191970')
      .trackColor('#ADD8E6')
      .selectedColor('#4169E1')
      .showTips(true)
      .onChange((value: number, mode: SliderChangeMode) => {
        this.inSetValueOne = value
        console.info('value:' + value + 'mode:' + mode.toString())
      })
    Text(this.inSetValueOne.toFixed(0)).fontSize(12)
  }
  .width('80%')

  Row() {
    Slider({
      value: this.inSetValueTwo,
      step: 10,
      style: SliderStyle.InSet
    })
      .blockColor('#191970')
      .trackColor('#ADD8E6')
      .selectedColor('#4169E1')
      .showSteps(true)
      .onChange((value: number, mode: SliderChangeMode) => {
        this.inSetValueTwo = value
        console.info('value:' + value + 'mode:' + mode.toString())
      })

    Text(this.inSetValueTwo.toFixed(0)).fontSize(12)
  }
  .width('80%')

  Row() {
    Column() {
      Text('vertical outset slider').fontSize(9).fontColor(0xCCCCCC).width('50%').margin(15)
      Row() {
        Text().width('10%')
        Slider({
          value: this.vOutSetValueOne,
          style: SliderStyle.OutSet,
          direction: Axis.Vertical
        })
          .blockColor('#191970')
          .trackColor('#ADD8E6')
          .selectedColor('#4169E1')
          .showTips(true)
          .onChange((value: number, mode: SliderChangeMode) => {
            this.vOutSetValueOne = value
            console.info('value:' + value + 'mode:' + mode.toString())
          })

        Slider({
          value: this.vOutSetValueTwo,
          step: 10,
          style: SliderStyle.OutSet,
          direction: Axis.Vertical
        })
          .blockColor('#191970')
          .trackColor('#ADD8E6')
          .selectedColor('#4169E1')
          .showSteps(true)
          .onChange((value: number, mode: SliderChangeMode) => {
            this.vOutSetValueTwo = value
            console.info('value:' + value + 'mode:' + mode.toString())
          })
      }
    }.width('50%').height(300)

    Column() {
      Text('vertical inset slider').fontSize(9).fontColor(0xCCCCCC).width('50%').margin(15)
      Row() {
        Slider({
          value: this.vInSetValueOne,
          style: SliderStyle.InSet,
          direction: Axis.Vertical,
          reverse: true // 竖向的Slider默认是上端是min值,下端是max值,因此想要从下往上滑动,需要设置reverse为true
        })
          .showTips(true)
          .onChange((value: number, mode: SliderChangeMode) => {
            this.vInSetValueOne = value
            console.info('value:' + value + 'mode:' + mode.toString())
          })

        Slider({
          value: this.vInSetValueTwo,
          step: 10,
          style: SliderStyle.InSet,
          direction: Axis.Vertical,
          reverse: true
        })
          .showSteps(true)
          .onChange((value: number, mode: SliderChangeMode) => {
            this.vInSetValueTwo = value
            console.info('value:' + value + 'mode:' + mode.toString())
          })
      }
    }.width('50%').height(300)
  }
}.width('100%')

} }

更多关于HarmonyOS 鸿蒙Next中slider怎么让已滑动部分在中间开始,并且可以左右滑动?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


你的意思是滑动多少,横条展示多少吗
这是示例代码,你可以看看

@Entry
@Component
struct SliderExample {
  @State outSetValueOne: number = 40
  @State inSetValueOne: number = 40
  @State outSetValueTwo: number = 40
  @State inSetValueTwo: number = 40
  @State vOutSetValueOne: number = 40
  @State vInSetValueOne: number = 40
  @State vOutSetValueTwo: number = 40
  @State vInSetValueTwo: number = 40
  build() {
    Column({ space: 8 }) {
      Text('outset slider').fontSize(9).fontColor(0xCCCCCC).width('90%').margin(15)
      Row() {
        Slider({
          value: this.outSetValueOne,
          min: 0,
          max: 100,
          style: SliderStyle.OutSet
        })
        .showTips(true)
        .onChange((value: number, mode: SliderChangeMode) => {
          this.outSetValueOne = value
          console.info('value:' + value + 'mode:' + mode.toString())
        })
        // toFixed(0)将滑动条返回值处理为整数精度
        Text(this.outSetValueOne.toFixed(0)).fontSize(12)
      }
      .width('80%')
      Row() {
        Slider({
          value: this.outSetValueTwo,
          step: 10,
          style: SliderStyle.OutSet
        })
        .showSteps(true)
        .onChange((value: number, mode: SliderChangeMode) => {
          this.outSetValueTwo = value
          console.info('value:' + value + 'mode:' + mode.toString())
        })
        Text(this.outSetValueTwo.toFixed(0)).fontSize(12)
      }
      .width('80%')
      Text('inset slider').fontSize(9).fontColor(0xCCCCCC).width('90%').margin(15)
      Row() {
        Slider({
          value: this.inSetValueOne,
          min: 0,
          max: 100,
          style: SliderStyle.InSet
        })
        .blockColor('#191970')
        .trackColor('#ADD8E6')
        .selectedColor('#4169E1')
        .showTips(true)
        .onChange((value: number, mode: SliderChangeMode) => {
          this.inSetValueOne = value
          console.info('value:' + value + 'mode:' + mode.toString())
        })
        Text(this.inSetValueOne.toFixed(0)).fontSize(12)
      }
      .width('80%')
      Row() {
        Slider({
          value: this.inSetValueTwo,
          step: 10,
          style: SliderStyle.InSet
        })
        .blockColor('#191970')
        .trackColor('#ADD8E6')
        .selectedColor('#4169E1')
        .showSteps(true)
        .onChange((value: number, mode: SliderChangeMode) => {
          this.inSetValueTwo = value
          console.info('value:' + value + 'mode:' + mode.toString())
        })
        Text(this.inSetValueTwo.toFixed(0)).fontSize(12)
      }
      .width('80%')
      Row() {
        Column() {
          Text('vertical outset slider').fontSize(9).fontColor(0xCCCCCC).width('50%').margin(15)
          Row() {
            Text().width('10%')
            Slider({
              value: this.vOutSetValueOne,
              style: SliderStyle.OutSet,
              direction: Axis.Vertical
            })
            .blockColor('#191970')
            .trackColor('#ADD8E6')
            .selectedColor('#4169E1')
            .showTips(true)
            .onChange((value: number, mode: SliderChangeMode) => {
              this.vOutSetValueOne = value
              console.info('value:' + value + 'mode:' + mode.toString())
            })
            Slider({
              value: this.vOutSetValueTwo,
              step: 10,
              style: SliderStyle.OutSet,
              direction: Axis.Vertical
            })
            .blockColor('#191970')
            .trackColor('#ADD8E6')
            .selectedColor('#4169E1')
            .showSteps(true)
            .onChange((value: number, mode: SliderChangeMode) => {
              this.vOutSetValueTwo = value
              console.info('value:' + value + 'mode:' + mode.toString())
            })
          }
        }
        .width('50%').height(300)
        Column() {
          Text('vertical inset slider').fontSize(9).fontColor(0xCCCCCC).width('50%').margin(15)
          Row() {
            Slider({
              value: this.vInSetValueOne,
              style: SliderStyle.InSet,
              direction: Axis.Vertical,
              reverse: true // 竖向的Slider默认是上端是min值,下端是max值,因此想要从下往上滑动,需要设置reverse为true
            })
            .showTips(true)
            .onChange((value: number, mode: SliderChangeMode) => {
              this.vInSetValueOne = value
              console.info('value:' + value + 'mode:' + mode.toString())
            })
            Slider({
              value: this.vInSetValueTwo,
              step: 10,
              style: SliderStyle.InSet,
              direction: Axis.Vertical,
              reverse: true
            })
            .showSteps(true)
            .onChange((value: number, mode: SliderChangeMode) => {
              this.vInSetValueTwo = value
              console.info('value:' + value + 'mode:' + mode.toString())
            })
          }
        }
        .width('50%').height(300)
      }
    }
    .width('100%')
  }
}

如果想要从中间开始,状态变量设置为中间值就可以,

我的意思是已滑动部分的起点是在中间而不是通常的左边,而且在中间可以左右滑动,

那你需要写一个按钮,外面有个包住他容器组件,然后通过手势来改变他的定位。

在HarmonyOS鸿蒙Next中,要让Slider组件的已滑动部分从中间开始,并且可以左右滑动,可以通过设置Slider的初始值和范围来实现。

  1. 设置Slider的初始值:确保Slider的初始值位于其最小值与最大值的中点。例如,如果Slider的范围是从0到100,那么你可以将初始值设置为50。

  2. 确保Slider可左右滑动:通常Slider组件默认就是可以左右(或上下,取决于方向)滑动的,只要没有额外的限制条件。但你需要确保在布局中没有限制Slider的滑动范围或方向。

具体实现步骤如下:

  • 在XML布局文件中定义Slider组件,并设置minmaxvalue属性。例如:
<Slider
    ohos:id="$+id:slider"
    ohos:width="match_parent"
    ohos:height="wrap_content"
    ohos:min="0"
    ohos:max="100"
    ohos:value="50"/>
  • 在JavaScript(或其他支持的语言,如ETS)代码中,不需要额外设置即可实现从中间开始滑动的效果。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部