当前位置:   article > 正文

17.鸿蒙HarmonyOS App(JAVA)滑动选择器_鸿蒙滚轮选择器

鸿蒙滚轮选择器

每天进步一点点,成功在久不在速

滑动选择器

 

 

 //设置文本样式

picker.setNormalTextFont(Font.DEFAULT_BOLD);
picker.setNormalTextSize(40);
picker.setNormalTextColor(new Color(Color.getIntColor("#FFA500")));
picker.setSelectedTextFont(Font.DEFAULT_BOLD);
picker.setSelectedTextSize(40);
picker.setSelectedTextColor(new Color(Color.getIntColor("#00FFFF")));

//设置边框

ShapeElement shape = new ShapeElement();
shape.setShape(ShapeElement.RECTANGLE);
shape.setRgbColor(RgbColor.fromArgbInt(0xFF40E0D0));
// 单独设置上边框
// picker.setDisplayedLinesTopElement(shape); 
// 单独设置下边框
// picker.setDisplayedLinesBottomElement(shape);
// 同时设置上下边框
picker.setDisplayedLinesElements(shape, shape); 

设置着色器

<Picker
    ohos:id="$+id:picker2"
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:normal_text_size="20vp"
    ohos:selected_text_size="26vp"
    ohos:top_margin="10vp"
    ohos:max_value="33"
    ohos:min_value="16"
    ohos:background_element="#686EA8EF"
    ohos:shader_color="#FF1EFF34"
    />

ability_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <DirectionalLayout
  3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
  4. ohos:height="match_parent"
  5. ohos:width="match_parent"
  6. ohos:alignment="center"
  7. ohos:orientation="vertical">
  8. <Text
  9. ohos:id="$+id:text_helloworld3"
  10. ohos:height="match_content"
  11. ohos:width="match_content"
  12. ohos:background_element="$graphic:background_ability_main"
  13. ohos:layout_alignment="horizontal_center"
  14. ohos:text="每天进步一点点"
  15. ohos:text_size="40vp"
  16. />
  17. <Text
  18. ohos:id="$+id:text_helloworld3a"
  19. ohos:height="match_content"
  20. ohos:width="match_content"
  21. ohos:background_element="#FFA5F383"
  22. ohos:layout_alignment="horizontal_center"
  23. ohos:text="成功在久不在速"
  24. ohos:text_size="40vp"
  25. />
  26. <Text
  27. ohos:id="$+id:text_helloworld"
  28. ohos:height="match_content"
  29. ohos:width="match_content"
  30. ohos:background_element="$graphic:background_ability_main"
  31. ohos:layout_alignment="horizontal_center"
  32. ohos:text="$string:mainability_HelloWorld"
  33. ohos:text_size="40vp"
  34. />
  35. <Text
  36. ohos:id="$+id:text_helloworld2"
  37. ohos:height="match_content"
  38. ohos:width="match_content"
  39. ohos:background_element="#FFA5F383"
  40. ohos:layout_alignment="horizontal_center"
  41. ohos:text="滑动选择器"
  42. ohos:text_size="40vp"
  43. />
  44. <Picker
  45. ohos:id="$+id:picker2"
  46. ohos:height="match_content"
  47. ohos:width="match_parent"
  48. ohos:normal_text_size="20vp"
  49. ohos:selected_text_size="26vp"
  50. ohos:top_margin="10vp"
  51. ohos:max_value="33"
  52. ohos:min_value="16"
  53. ohos:background_element="#686EA8EF"
  54. ohos:shader_color="#FF1EFF34"
  55. />
  56. <Picker
  57. ohos:id="$+id:picker2a"
  58. ohos:height="match_content"
  59. ohos:width="match_parent"
  60. ohos:normal_text_size="20vp"
  61. ohos:selected_text_size="26vp"
  62. ohos:top_margin="10vp"
  63. ohos:background_element="#686EA8EF"
  64. />
  65. </DirectionalLayout>

 MainAbilitySlice.java

  1. package com.example.myapplication.slice;
  2. import com.example.myapplication.ResourceTable;
  3. import ohos.aafwk.ability.AbilitySlice;
  4. import ohos.aafwk.content.Intent;
  5. import ohos.agp.colors.Color;
  6. import ohos.agp.colors.RgbColor;
  7. import ohos.agp.components.Picker;
  8. import ohos.agp.components.element.Element;
  9. import ohos.agp.components.element.ShapeElement;
  10. public class MainAbilitySlice extends AbilitySlice {
  11. @Override
  12. public void onStart(Intent intent) {
  13. super.onStart(intent);
  14. super.setUIContent(ResourceTable.Layout_ability_main);
  15. //对滑动选择器进行配置
  16. Picker picker = (Picker) findComponentById(ResourceTable.Id_picker2);
  17. picker.setCompoundElementPadding(50);//设置文本左右两侧的图形元素边距
  18. picker.setSelectorItemNum(3);//选项显示的数量
  19. 设置文本左右两侧的图形元素
  20. picker.setElementFormatter(new Picker.ElementFormatter() {
  21. @Override
  22. public Element leftElement(int i) { //左侧
  23. ShapeElement element = new ShapeElement();
  24. element.setRgbColor(new RgbColor(0,0,255));
  25. return element;
  26. }
  27. @Override
  28. public Element rightElement(int i) {
  29. return null;
  30. }
  31. });
  32. //将索引转换为格式化文本
  33. picker.setFormatter(new Picker.Formatter() {
  34. @Override
  35. public String format(int i) {
  36. return "选项:"+i;
  37. }
  38. });
  39. //批量设置字符串方式显示
  40. Picker picker2a = (Picker) findComponentById(ResourceTable.Id_picker2a);
  41. picker2a.setCompoundElementPadding(50);//设置文本左右两侧的图形元素边距
  42. picker2a.setSelectorItemNum(3);//选项显示的数量
  43. picker2a.setDisplayedData(new String[]{"上海","北京","山东","江苏","海南"});
  44. }
  45. @Override
  46. public void onActive() {
  47. super.onActive();
  48. }
  49. @Override
  50. public void onForeground(Intent intent) {
  51. super.onForeground(intent);
  52. }
  53. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/274929
推荐阅读
相关标签
  

闽ICP备14008679号