HarmonyOS实战—TextField文本输入框组件基本使用

想了解更多内容,请访问:

网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于成都企业网站定制,高端网页制作,对高空作业车租赁等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业seo优化排名优化,H5建站,响应式网站。

和华为官方合作共建的鸿蒙技术社区

https://harmonyos.

1. TextField组件基本用法

组件说明:

  • 是Text的子类,用来进行用户输入数据的。

常见属性:

 
 
 
 
  1.         ohos:id="$+id:text" 
  2.         ohos:height="50vp" 
  3.         ohos:width="319vp" 
  4.         ohos:background_element="#FFFFFF" 
  5.         ohos:hint="请输入信息" 
  6.         ohos:layout_alignment="horizontal_center" 
  7.         ohos:text_alignment="center" 
  8.         ohos:text_color="#999999" 
  9.         ohos:text_size="17fp" 
  10.         ohos:top_margin="100vp"/> 

2. TextField案例——获取文本输入框中的内容并进行Toast提示

  • 通过TextField获取文本输入框中的内容并进行Toast提示
  • 新建项目:TextFieldApplication

ability_main

 
 
 
 
  1.  
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="match_parent" 
  4.     ohos:width="match_parent" 
  5.     ohos:background_element="#F2F2F2" 
  6.     ohos:orientation="vertical"> 
  7.  
  8.     
  9.         ohos:id="$+id:text" 
  10.         ohos:height="50vp" 
  11.         ohos:width="319vp" 
  12.         ohos:background_element="#FFFFFF" 
  13.         ohos:hint="请输入信息" 
  14.         ohos:layout_alignment="horizontal_center" 
  15.         ohos:text_alignment="center" 
  16.         ohos:text_color="#999999" 
  17.         ohos:text_size="17fp" 
  18.         ohos:top_margin="100vp"/> 
  19.  
  20.     
  21.         ohos:id="$+id:but" 
  22.         ohos:height="47vp" 
  23.         ohos:width="319vp" 
  24.         ohos:background_element="#21a8FD" 
  25.         ohos:layout_alignment="center" 
  26.         ohos:text="获取信息" 
  27.         ohos:text_alignment="center" 
  28.         ohos:text_color="#FEFEFE" 
  29.         ohos:text_size="24vp" 
  30.         ohos:top_margin="77vp"/> 
  31.  
  32.  

因为要在 onClick 方法中用到 TextField 和 Button 这两个组件,所以要把这两个组件移到成员位置,使其成为成员变量后,onClick 方法才能访问的到。

MainAbilitySlice

 
 
 
 
  1. package com.xdr630.textfieldapplication.slice; 
  2.  
  3. import com.xdr630.textfieldapplication.ResourceTable; 
  4. import ohos.aafwk.ability.AbilitySlice; 
  5. import ohos.aafwk.content.Intent; 
  6. import ohos.agp.components.Button; 
  7. import ohos.agp.components.Component; 
  8. import ohos.agp.components.TextField; 
  9. import ohos.agp.utils.LayoutAlignment; 
  10. import ohos.agp.window.dialog.ToastDialog; 
  11.  
  12. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener { 
  13.  
  14.     TextField tf; 
  15.     Button but; 
  16.  
  17.     @Override 
  18.     public void onStart(Intent intent) { 
  19.         super.onStart(intent); 
  20.         super.setUIContent(ResourceTable.Layout_ability_main); 
  21.  
  22.         //1.找到文本组件框对象 
  23.         tf = (TextField) findComponentById(ResourceTable.Id_text); 
  24.         //找到按钮组件对象 
  25.         but = (Button) findComponentById(ResourceTable.Id_but); 
  26.  
  27.         //2.给按钮绑定点击事件 
  28.         //当点击了按钮之后,就要获取文本输入框的内容 
  29.         but.setClickedListener(this); 
  30.  
  31.     } 
  32.  
  33.     @Override 
  34.     public void onActive() { 
  35.         super.onActive(); 
  36.     } 
  37.  
  38.     @Override 
  39.     public void onForeground(Intent intent) { 
  40.         super.onForeground(intent); 
  41.     } 
  42.  
  43.     @Override 
  44.     public void onClick(Component component) { 
  45.         //当点击了按钮之后,获取文本输入框的内容 
  46.         String message = tf.getText(); 
  47.         //利用一个Toast将信息弹出 
  48.         ToastDialog td = new ToastDialog(this); 
  49.         //大小不用设置,默认是包裹内容的 
  50.         //自动关闭不用设置,默认到了时间之后就自动关闭 
  51.         //默认持续时间是 2秒 
  52.  
  53.         //设置Toast的背景 
  54.         td.setTransparent(true); 
  55.         //位置(默认居中) 
  56.         td.setAlignment(LayoutAlignment.BOTTOM); 
  57.         //设置一个偏移 
  58.         td.setOffset(0,200); 
  59.         //设置Toast内容 
  60.         td.setText(message); 
  61.         //让Toast出现 
  62.         td.show(); 
  63.     } 

运行:

3. TextField组件高级用法

3.1 密码的密文展示

当输入密码的时候会变成密文展示。

ohos:text_input_type="pattern_password":表示输入的密码以密文的方式显示。

基本使用:

 
 
 
 
  1.  
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="match_parent" 
  4.     ohos:width="match_parent" 
  5.     ohos:orientation="vertical" 
  6.     ohos:background_element="#F2F2F2"> 
  7.  
  8.     
  9.         ohos:height="50vp" 
  10.         ohos:width="319vp" 
  11.         ohos:hint="请输入信息" 
  12.         ohos:text_size="17fp" 
  13.         ohos:hint_color="#999999" 
  14.         ohos:text_alignment="center" 
  15.         ohos:top_margin="100vp" 
  16.         ohos:layout_alignment="horizontal_center" 
  17.         ohos:background_element="#FFFFFF" 
  18.         ohos:text_input_type="pattern_password"/> 
  19.  
  20.  

3.2 基线的设置

有的时候文本输入框并不是一个框,而是下面有一条横线,这条线华为官方叫做基线。

把文本输入框使用横线表示,在上面加上一条基线,把输入框的背景颜色去掉。

 
 
 
 
  1.         ohos:height="50vp" 
  2.         ohos:width="319vp" 
  3.         ohos:hint="请输入信息" 
  4.         ohos:text_size="17fp" 
  5.         ohos:hint_color="#999999" 
  6.         ohos:text_alignment="center" 
  7.         ohos:top_margin="100vp" 
  8.         ohos:layout_alignment="horizontal_center" 
  9.         ohos:text_input_type="pattern_password" 
  10.         ohos:basement="#000000" 
  11.         /> 

如果以后看到一条基线,然后在输入一些数字信息,这还是 TextField 文本输入框组件,只不过是背景色没有设置,让它跟布局的颜色一致了,看不到背景而已。

3.3 气泡的设置

当用鼠标长按选中输入的内容后,就会选中内容,前面的光标和后面的光标,以及中间选中的内容颜色会改变,华为官方给前、后的光标,以及没有选中内容状态下出现的小气球取名为气泡。

 
 
 
 
  1.         ohos:height="50vp" 
  2.         ohos:width="319vp" 
  3.         ohos:hint="请输入信息" 
  4.         ohos:text_size="17fp" 
  5.         ohos:hint_color="#999999" 
  6.         ohos:text_alignment="center" 
  7.         ohos:top_margin="100vp" 
  8.         ohos:layout_alignment="horizontal_center" 
  9.         ohos:basement="#000000" 
  10.         /> 

可以设置左边、右边,以及没有选中情况下的气泡。

气泡的图片、颜色都是可以自定义的。

以下用到的图片可自取:https://www.aliyundrive.com/s/wT22d1Vb1BV

把左、右,以及中间没有选中的气泡图片复制到 media 文件夹下。

 
 
 
 
  1.         ohos:height="50vp" 
  2.         ohos:width="319vp" 
  3.         ohos:hint="请输入信息" 
  4.         ohos:text_size="17fp" 
  5.         ohos:hint_color="#999999" 
  6.         ohos:text_alignment="center" 
  7.         ohos:top_margin="100vp" 
  8.         ohos:layout_alignment="horizontal_center" 
  9.         ohos:basement="#000000" 
  10.         ohos:element_selection_left_bubble="$media:left" 
  11.         ohos:element_selection_right_bubble="$media:right" 
  12.         ohos:element_cursor_bubble="$media:bubble" 
  13.         ohos:selection_color="#FF0000" 
  14.         /> 
  • ohos:element_selection_left_bubble、ohos:element_selection_right_bubble分别设置左右气泡显示的图片
  • ohos:element_cursor_bubble:设置没有选中时的气泡图片
  • ohos:selection_color:设置选中时内容的颜色

运行:

4. TextField案例——长按查看密码明文

在一些APP中,登录界面密码输入框那里有个小眼睛,按住小眼睛后就可以看到密码的明文展示,松开小眼睛又恢复到密文状态了。

把“小眼睛”改成Button组件,实现的逻辑原理也是一样的。

需求分析:

  • 按住按钮不松,将输入框中的密码变成明文
  • 松开按钮之后,输入框中的密码变回密文

新建项目:TextFieldApplication3

ability_main

 
 
 
 
  1.  
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="match_parent" 
  4.     ohos:width="match_parent" 
  5.     ohos:orientation="vertical" 
  6.     ohos:background_element="#F2F2F2" 
  7.     > 
  8.  
  9.     
  10.         ohos:id="$+id:text" 
  11.         ohos:height="50vp" 
  12.         ohos:width="319vp" 
  13.         ohos:hint="请输入密码" 
  14.         ohos:text_size="17fp" 
  15.         ohos:hint_color="#999999" 
  16.         ohos:text_alignment="center" 
  17.         ohos:top_margin="100vp" 
  18.         ohos:layout_alignment="horizontal_center" 
  19.         ohos:background_element="#FFFFFF" 
  20.         ohos:text_input_type="pattern_password"/> 
  21.      
  22.     
  23.         ohos:id="$+id:but" 
  24.         ohos:height="47vp" 
  25.         ohos:width="319vp" 
  26.         ohos:text="查看密码" 
  27.         ohos:text_size="24vp" 
  28.         ohos:text_color="#FEFEFE" 
  29.         ohos:text_alignment="center" 
  30.         ohos:background_element="#21a8FD" 
  31.         ohos:top_margin="77vp" 
  32.         ohos:layout_alignment="center"/> 
  33.  
  34.  

 MainAbilitySlice

 
 
 
 
  1. package com.xdr630.textfieldapplication3.slice; 
  2.  
  3. import com.xdr630.textfieldapplication3.ResourceTable; 
  4. import ohos.aafwk.ability.AbilitySlice; 
  5. import ohos.aafwk.content.Intent; 
  6. import ohos.agp.components.Button; 
  7. import ohos.agp.components.Component; 
  8. import ohos.agp.components.InputAttribute; 
  9. import ohos.agp.components.TextField; 
  10. import ohos.multimodalinput.event.TouchEvent; 
  11.  
  12. public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener { 
  13.  
  14.     TextField tf; 
  15.  
  16.     @Override 
  17.     public void onStart(Intent intent) { 
  18.         super.onStart(intent); 
  19.         super.setUIContent(ResourceTable.Layout_ability_main); 
  20.  
  21.         //1.找到两个组件对象 
  22.         tf = (TextField) findComponentById(ResourceTable.Id_text); 
  23.         Button but = (Button) findComponentById(ResourceTable.Id_but); 
  24.  
  25.         //2.要给按钮绑定一个触摸事件 
  26.         //因为在触摸事件中,才能获取到按下不松或松开 
  27.         //单击事件——只能捕获到点击了一下 
  28.         but.setTouchEventListener(this); 
  29.  
  30.  
  31.     } 
  32.  
  33.     @Override 
  34.     public void onActive() { 
  35.         super.onActive(); 
  36.     } 
  37.  
  38.     @Override 
  39.     public void onForeground(Intent intent) { 
  40.         super.onForeground(intent); 
  41.     } 
  42.  
  43.     @Override 
  44.     //参数一:现在触摸的按钮 
  45.     //参数二:动作对象 
  46.     public boolean onTouchEvent(Component component, TouchEvent touchEvent) { 
  47.         int action = touchEvent.getAction(); 
  48.  
  49.         if (action == TouchEvent.PRIMARY_POINT_DOWN){//表示按下不松的时候 
  50.             //当按下不送的时候,将文本框中密码变成明文 
  51.             tf.setTextInputType(InputAttribute.PATTERN_NULL); 
  52.         }else if (action == TouchEvent.PRIMARY_POINT_UP){//表示松开的时候 
  53.             //当松开的时候,将文本框中的密码变回密文 
  54.             tf.setTextInputType(InputAttribute.PATTERN_PASSWORD); 
  55.         } 
  56.         //true:表示触摸事件的后续动作还会进行触发 
  57.         //false:表示触摸事件只触发第一个按下不松 
  58.         return true; 
  59.     } 

 运行:

5. TextField案例——搭建登录界面

新建项目:TextFieldApplication4

细节说明:

  • Text文本(忘记密码了?)组件默认是左边放置的,加上 ohos:layout_alignment="right"就是右边放置了,同时也给个ohos:right_margin="20vp"和右边的屏幕有些距离。如果ohos:layout_alignment="right"属性不写,直接写ohos:right_margin="20vp,那么ohos:layout_alignment="right"属性就会失效,因为组件默认是放在左边的。

ability_main

 
 
 
 
  1.  
  2.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  3.     ohos:height="match_parent" 
  4.     ohos:width="match_parent" 
  5.     ohos:orientation="vertical" 
  6.     ohos:background_element="#F2F2F2"> 
  7.  
  8.     
  9.         ohos:id="$+id:username" 
  10.         ohos:height="50vp" 
  11.         ohos:width="319vp" 
  12.         ohos:hint="请输入用户名" 
  13.         ohos:text_size="17fp" 
  14.         ohos:hint_color="#999999" 
  15.         ohos:text_alignment="center" 
  16.         ohos:top_margin="100vp" 
  17.         ohos:layout_alignment="horizontal_center" 
  18.         ohos:background_element="#FFFFFF"/> 
  19.  
  20.     
  21.         ohos:id="$+id:password" 
  22.         ohos:height="50vp" 
  23.         ohos:width="319vp" 
  24.         ohos:hint="请输入密码" 
  25.         ohos:text_size="17fp" 
  26.         ohos:hint_color="#999999" 
  27.         ohos:text_alignment="center" 
  28.         ohos:top_margin="10vp" 
  29.         ohos:layout_alignment="horizontal_center" 
  30.         ohos:background_element="#FFFFFF" 
  31.         ohos:text_input_type="pattern_password"/> 
  32.      
  33.     
  34.         ohos:height="match_content" 
  35.         ohos:width="match_content" 
  36.         ohos:text="忘记密码了?" 
  37.         ohos:text_size="17fp" 
  38.         ohos:text_color="#979797" 
  39.         ohos:top_margin="13vp" 
  40.         ohos:layout_alignment="right" 
  41.         ohos:right_margin="20vp"/> 
  42.      
  43.     
  44.         ohos:height="47vp" 
  45.         ohos:width="319vp" 
  46.         ohos:text="登录" 
  47.         ohos:text_size="24fp" 
  48.         ohos:text_color="#FEFEFE" 
  49.         ohos:text_alignment="center" 
  50.         ohos:background_element="#21a8FD" 
  51.         ohos:top_margin="77vp" 
  52.         ohos:layout_alignment="horizontal_center"/> 
  53.  
  54.     
  55.         ohos:height="47vp" 
  56.         ohos:width="319vp" 
  57.         ohos:text="注册" 
  58.         ohos:text_size="24fp" 
  59.         ohos:text_color="#FEFEFE" 
  60.         ohos:text_alignment="center" 
  61.         ohos:background_element="#21a8FD" 
  62.         ohos:top_margin="13vp" 
  63.         ohos:layout_alignment="horizontal_center"/> 
  64.  
  65.  
  66.  
  67.  

 运行:

想了解更多内容,请访问:

和华为官方合作共建的鸿蒙技术社区

https://harmonyos.

网页标题:HarmonyOS实战—TextField文本输入框组件基本使用
文章URL:http://www.csdahua.cn/qtweb/news40/66140.html

网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网