当前位置:   article > 正文

HarmonyOS开发45:Picker滑动选择器组件基本用法_@ohos.file.picker

@ohos.file.picker

基本用法:

picker是滑动选择器组件。在一些app中选择地址的时候会用到,但是一般是三个picker选择器组合在一起使用。

如图所示:

在这里插入图片描述

ability_main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent"
    ohos:alignment="center"
    ohos:orientation="vertical">

    <Picker
        ohos:id="$+id:picker"
        ohos:height="match_content"
        ohos:width="100vp"
        ohos:max_value="6"
        ohos:min_value="0"
        ohos:normal_text_color="#FF299CB8"
        ohos:normal_text_size="20fp"
        ohos:selected_text_color="#FF0000"
        ohos:selected_text_size="20fp"
        ohos:value="0"
        />
    <!--ohos:shader_color="#00FF00"着色器-->

</DirectionalLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

MainAbilitySlice.java代码:

package com.example.pickerapplication.slice;

import com.example.pickerapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Picker;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        //1.找到组件
        Picker picker = (Picker) findComponentById(ResourceTable.Id_picker);

        //把要真实的内容全部放在集合中
        ArrayList<String> list = new ArrayList<>();
        list.add("星期一");
        list.add("星期二");
        list.add("星期三");
        list.add("星期四");
        list.add("星期五");
        list.add("星期六");
        list.add("星期日");

        //2.设置展示内容
       /* picker.setFormatter(
                new Picker.Formatter() {
                    @Override
                    public String format(int i) {
                        //其中参数i表示当前选择的数字
                        //返回值:展示的内容
                        // return "星期" + (i + 1);
                        return list.get(i);
                    }
                }
        );*/

        //优化
        /*picker.setFormatter(
                (int i) -> {
                    return list.get(i);
                }
        );*/

        //再优化
        //picker.setFormatter(i -> list.get(i));

        //再再优化:还可以用方法引用优化代码
        picker.setFormatter(list::get);

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 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
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签