当前位置:   article > 正文

【鸿蒙】HarMonyOS之TextField组件的常用属性_"a、ohos:multiple_lines =\"false"

"a、ohos:multiple_lines =\"false"

TextField提供了一种文本输入框。

支持的XML属性

TextField的共有XML属性继承自:Text

TextField的自有XML属性见下表:

表1 TextField的自有XML属性

属性名称

中文描述

取值

取值说明

使用案例

basement

输入框基线

Element类型

可直接配置色值,也可引用color资源或引用media/graphic下的图片资源。

ohos:basement="#000000"

ohos:basement="$color:black"

ohos:basement="$media:media_src"

ohos:basement="$graphic:graphic_src"

创建TextField

在layout目录下的xml文件中创建一个TextField。

  1. <TextField
  2. ...
  3. ohos:height="40vp"
  4. ohos:width="200vp"
  5. ohos:left_padding="20vp"
  6. />

获取输入框的内容:

String content = textField.getText();

设置TextField

  • 在xml中设置TextField的背景。

    layout目录下xml文件的代码示例如下:

  1. <TextField
  2. ...
  3. ohos:background_element="$graphic:background_text_field"
  4. />

graphic目录下xml文件(例:background_text_field.xml)的代码示例如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <corners
  5. ohos:radius="40"/>
  6. <solid
  7. ohos:color="#FFFFFF"/>
  8. </shape>

设置提示文字

  1. <TextField
  2. ...
  3. ohos:hint="Enter phone number or email"
  4. ohos:text_alignment="vertical_center"/>
  • 图1 创建TextField效果

  • 设置Bubble
  1. <TextField
  2. ...
  3. ohos:element_cursor_bubble="$graphic:ele_cursor_bubble" />

其中ele_cursor_bubble.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <corners
  5. ohos:radius="40"/>
  6. <solid
  7. ohos:color="#6699FF"/>
  8. <stroke
  9. ohos:color="#0066FF"
  10. ohos:width="10"/>
  11. </shape>
  • 图2 设置bubble的效果

  • 设置TextField的内边距
    1. <TextField
    2. ...
    3. ohos:left_padding="24vp"
    4. ohos:right_padding="24vp"
    5. ohos:top_padding="8vp"
    6. ohos:bottom_padding="8vp"/>

     

  • 设置TextField的多行显示
    1. <TextField
    2. ...
    3. ohos:multiple_lines="true"/>

     

  • 设置TextField不可用状态

    通过TextField的Enable属性来控制文本框是否可用,当设置成false后,文本框不再能被输入。

    1. TextField textField = (TextField) findComponentById(ResourceTable.Id_text_field);
    2. textField.setEnabled(false);

     

  • 响应焦点变化
    1. textField.setFocusChangedListener((component, isFocused) -> {
    2. if (isFocused) {
    3. // 获取到焦点
    4. ...
    5. } else {
    6. // 失去焦点
    7. ...
    8. }
    9. });

     

  • 设置基线
    1. <TextField
    2. ...
    3. ohos:basement="#000099" />

    图3 设置基线的效果

  •  

场景示例

当点击登录按钮,将会出现错误提示,同时将会改变TextField的状态。

图4 演示TextField错误提示效果

  • ability_text_field.xml代码示例:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <DirectionalLayout
    3. xmlns:ohos="http://schemas.huawei.com/res/ohos"
    4. ohos:width="match_parent"
    5. ohos:height="match_parent"
    6. ohos:background_element="#FF000000"
    7. ohos:orientation="vertical">
    8. <StackLayout
    9. ohos:top_margin="60vp"
    10. ohos:width="match_parent"
    11. ohos:height="match_content"
    12. ohos:layout_alignment="center">
    13. <TextField
    14. ohos:id="$+id:name_textField"
    15. ohos:width="600vp"
    16. ohos:height="match_content"
    17. ohos:multiple_lines="false"
    18. ohos:left_padding="24vp"
    19. ohos:right_padding="24vp"
    20. ohos:top_padding="8vp"
    21. ohos:bottom_padding="8vp"
    22. ohos:min_height="44vp"
    23. ohos:text_size="18fp"
    24. ohos:layout_alignment="center"
    25. ohos:text_alignment="vertical_center"
    26. ohos:background_element="$graphic:background_text_field"
    27. ohos:hint="Enter phone number or email" />
    28. <Text
    29. ohos:visibility="hide"
    30. ohos:id="$+id:error_tip_text"
    31. ohos:width="match_content"
    32. ohos:height="match_content"
    33. ohos:top_padding="8vp"
    34. ohos:bottom_padding="8vp"
    35. ohos:right_margin="20vp"
    36. ohos:text="Incorrect account or password"
    37. ohos:text_size="18fp"
    38. ohos:text_color="red"
    39. ohos:layout_alignment="right"/>
    40. </StackLayout>
    41. <TextField
    42. ohos:top_margin="40vp"
    43. ohos:id="$+id:password_text_field"
    44. ohos:width="600vp"
    45. ohos:height="match_content"
    46. ohos:multiple_lines="false"
    47. ohos:left_padding="24vp"
    48. ohos:right_padding="24vp"
    49. ohos:top_padding="8vp"
    50. ohos:bottom_padding="8vp"
    51. ohos:min_height="44vp"
    52. ohos:text_size="18fp"
    53. ohos:layout_alignment="center"
    54. ohos:text_alignment="vertical_center"
    55. ohos:background_element="$graphic:background_text_field"
    56. ohos:hint="Enter password" />
    57. <Button
    58. ohos:top_margin="40vp"
    59. ohos:id="$+id:ensure_button"
    60. ohos:width="120vp"
    61. ohos:height="35vp"
    62. ohos:background_element="$graphic:background_btn"
    63. ohos:text="Log in"
    64. ohos:text_size="20fp"
    65. ohos:layout_alignment="horizontal_center"/>
    66. </DirectionalLayout>

    background_text_field.xml代码示例:

    1. <?xml version="1.0" encoding="UTF-8" ?>
    2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
    3. ohos:shape="rectangle">
    4. <corners
    5. ohos:radius="40"/>
    6. <solid
    7. ohos:color="white"/>
    8. <stroke
    9. ohos:color="black"
    10. ohos:width="6"/>
    11. </shape>

    background_btn.xml代码示例:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <corners
  5. ohos:radius="35"/>
  6. <solid
  7. ohos:color="white"/>
  8. </shape>
  • Java代码示例:
    1. // 当点击登录,改变相应组件的样式
    2. Button button = (Button) findComponentById(ResourceTable.Id_ensure_button);
    3. button.setClickedListener((component -> {
    4. // 显示错误提示的Text
    5. Text text = (Text) findComponentById(ResourceTable.Id_error_tip_text);
    6. text.setVisibility(Component.VISIBLE);
    7. // 显示TextField错误状态下的样式
    8. ShapeElement errorElement = new ShapeElement(this, ResourceTable.Graphic_background_text_field_error);
    9. TextField textField = (TextField) findComponentById(ResourceTable.Id_text_field);
    10. textField.setBackground(errorElement);
    11. // TextField失去焦点
    12. textField.clearFocus();
    13. }));

    其中background_text_field_error.xml代码示例:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
  3. ohos:shape="rectangle">
  4. <corners
  5. ohos:radius="40"/>
  6. <solid
  7. ohos:color="gray"/>
  8. <stroke
  9. ohos:color="#E74C3C"
  10. ohos:width="6"/>
  11. </shape>

 

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

闽ICP备14008679号