当前位置:   article > 正文

[HarmonyOS]——文本输入框组件(TextField)_harmonyos textfield

harmonyos textfield

文本输入框组件(TextField),是Text文本组件的子类

一、基本复现

点击按钮获取文本输入框的基本内容

基本属性:

  • hint:默认提示文本信息,相当于以往前端中的placeholder
  • hint-color:默认提示文本的颜色

Ability中定义组件:

  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:background_element="#f2f2f2"
  7. ohos:orientation="vertical">
  8. <!-- hint:默认提示信息 -->
  9. <TextField
  10. ohos:id="$+id:text"
  11. ohos:height="50vp"
  12. ohos:width="319vp"
  13. ohos:background_element="#FFFFFF"
  14. ohos:hint="请输入信息"
  15. ohos:hint_color="#999999"
  16. ohos:layout_alignment="horizontal_center"
  17. ohos:text_alignment="center"
  18. ohos:text_size="17fp"
  19. ohos:top_margin="100vp"
  20. />
  21. <Button
  22. ohos:id="$+id:but"
  23. ohos:height="47fp"
  24. ohos:width="319vp"
  25. ohos:background_element="#21a8fd"
  26. ohos:layout_alignment="center"
  27. ohos:text="获取信息"
  28. ohos:text_alignment="center"
  29. ohos:text_color="#fefefe"
  30. ohos:text_size="24fp"
  31. ohos:top_margin="50vp"
  32. />
  33. </DirectionalLayout>

Java代码撰写逻辑操纵组件:

  1. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {
  2. TextField tf = null;
  3. Button but = null;
  4. @Override
  5. public void onStart(Intent intent) {
  6. super.onStart(intent);
  7. super.setUIContent(ResourceTable.Layout_ability_main);
  8. //找到组件
  9. tf = (TextField) findComponentById(ResourceTable.Id_text);
  10. but = (Button) findComponentById(ResourceTable.Id_but);
  11. //绑定事件
  12. but.setClickedListener(this);
  13. }
  14. @Override
  15. public void onActive() {
  16. super.onActive();
  17. }
  18. @Override
  19. public void onForeground(Intent intent) {
  20. super.onForeground(intent);
  21. }
  22. @Override
  23. public void onClick(Component component) {
  24. //当点击了按钮后,获取文本输入框中的内容
  25. String text = tf.getText();
  26. //利用吐司弹出信息
  27. ToastDialog td = new ToastDialog(this);
  28. //吐司大小不用设置,默认是包裹内容的,持续时间默认是两秒
  29. //吐司的背景,透明
  30. td.setTransparent(true);
  31. //位置(居中)
  32. td.setAlignment(LayoutAlignment.BOTTOM);
  33. //设置一个偏移,X轴不动,Y轴稍微上面点
  34. td.setOffset(0, 200);
  35. //设置吐司的内容
  36. td.setText(text);
  37. //吐司弹框显示
  38. td.show();
  39. }
  40. }

 二、组件高级用法

1、密码密文显示

属性:text_input_type——》pattern_password

 2、基线显示

属性:ohos:basement="#000000" ,用来设置基线的颜色

 3、鼠标长按文本拷贝(气泡修改、背景色修改)

拷贝时显示的气泡是可以修改的,可以分别对左右的气泡进行自定义,属性分别为:

  • ohos:element_selection_left_bubble="Id",长按选中时左边显示的气泡
  • ohos:element_selection_right_bubble="Id",长按选中时右边显示的气泡
  • ohos:element_cursor_bubble="Id",默认输入时显示的气泡
  • ohos:selection_color="color hex",长按选中后选中区域的背景色
  • 具体赋值就是在资源列表中查找自己需要的,即自己需要图片放到media目录中

 三、密码铭文/密文切换

长按按钮,进行密码铭文与密文之间的切换,即:(1)当长按不松手的时候,将文本的密码变成铭文;(2)当松手后恢复成密文

  • 默认情况下进行密文显示

 Ability中定义组件:

  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:background_element="#f2f2f2"
  7. ohos:orientation="vertical">
  8. <!-- hint:默认提示信息 -->
  9. <TextField
  10. ohos:id="$+id:text"
  11. ohos:height="50vp"
  12. ohos:width="319vp"
  13. ohos:hint="请输入密码"
  14. ohos:hint_color="#999999"
  15. ohos:layout_alignment="horizontal_center"
  16. ohos:text_alignment="center"
  17. ohos:text_size="17fp"
  18. ohos:top_margin="100vp"
  19. ohos:basement="#000000"
  20. ohos:background_element="#FFFFFF"
  21. ohos:text_input_type="pattern_password"
  22. />
  23. <Button
  24. ohos:id="$+id:but"
  25. ohos:height="47fp"
  26. ohos:width="319vp"
  27. ohos:background_element="#21a8fd"
  28. ohos:layout_alignment="center"
  29. ohos:text="查看铭文密码"
  30. ohos:text_alignment="center"
  31. ohos:text_color="#fefefe"
  32. ohos:text_size="24fp"
  33. ohos:top_margin="50vp"
  34. />
  35. </DirectionalLayout>

Java中定义逻辑代码操纵组件:

  1. package com.example.demo1.slice;
  2. import com.example.demo1.ResourceTable;
  3. import com.example.demo1.dialogutils.MyDialog;
  4. import com.example.demo1.dialogutils.MyToast;
  5. import ohos.aafwk.ability.AbilitySlice;
  6. import ohos.aafwk.content.Intent;
  7. import ohos.agp.components.*;
  8. import ohos.agp.utils.LayoutAlignment;
  9. import ohos.agp.window.dialog.CommonDialog;
  10. import ohos.agp.window.dialog.IDialog;
  11. import ohos.agp.window.dialog.ToastDialog;
  12. import ohos.data.resultset.ResultSetIndexOutOfRangeException;
  13. import ohos.multimodalinput.event.TouchEvent;
  14. import java.util.ArrayList;
  15. import java.util.Random;
  16. public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener {
  17. TextField tf = null;
  18. Button but = null;
  19. @Override
  20. public void onStart(Intent intent) {
  21. super.onStart(intent);
  22. super.setUIContent(ResourceTable.Layout_ability_main);
  23. //找到组件
  24. tf = (TextField) findComponentById(ResourceTable.Id_text);
  25. but = (Button) findComponentById(ResourceTable.Id_but);
  26. //绑定触摸事件,为了获取长按不松手
  27. but.setTouchEventListener(this);
  28. }
  29. @Override
  30. public void onActive() {
  31. super.onActive();
  32. }
  33. @Override
  34. public void onForeground(Intent intent) {
  35. super.onForeground(intent);
  36. }
  37. @Override
  38. public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
  39. //获取当前动作
  40. int action = touchEvent.getAction();
  41. //动作处理
  42. if(action == TouchEvent.PRIMARY_POINT_DOWN) {
  43. //当长按不松手的时候,将文本的密码变成铭文
  44. //PATTERN_NULL:null就代表什么也不做,就让其文本显示为铭文了
  45. tf.setTextInputType(InputAttribute.PATTERN_NULL);
  46. } else if(action == TouchEvent.PRIMARY_POINT_UP) {
  47. //当松开的时候,将文本框中的密码变回密文
  48. tf.setTextInputType(InputAttribute.PATTERN_PASSWORD);
  49. }
  50. //返回true,代表触摸事件可以生效多次
  51. //返回false,代表仅仅对第一次动作生效
  52. return true;
  53. }
  54. }

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