自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS實(shí)戰(zhàn)—TextField文本輸入框組件基本使用

開(kāi)發(fā) 前端 OpenHarmony
Text文本(忘記密碼了?)組件默認(rèn)是左邊放置的,加上 ohos:layout_alignment="right"就是右邊放置了,同時(shí)也給個(gè)ohos:right_margin="20vp"和右邊的屏幕有些距離。

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

1. TextField組件基本用法

組件說(shuō)明:

  • 是Text的子類,用來(lái)進(jìn)行用戶輸入數(shù)據(jù)的。

常見(jiàn)屬性:

  1. <TextField 
  2.         ohos:id="$+id:text" 
  3.         ohos:height="50vp" 
  4.         ohos:width="319vp" 
  5.         ohos:background_element="#FFFFFF" 
  6.         ohos:hint="請(qǐng)輸入信息" 
  7.         ohos:layout_alignment="horizontal_center" 
  8.         ohos:text_alignment="center" 
  9.         ohos:text_color="#999999" 
  10.         ohos:text_size="17fp" 
  11.         ohos:top_margin="100vp"/> 

2. TextField案例——獲取文本輸入框中的內(nèi)容并進(jìn)行Toast提示

  • 通過(guò)TextField獲取文本輸入框中的內(nèi)容并進(jìn)行Toast提示
  • 新建項(xiàng)目:TextFieldApplication

ability_main

  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.  
  9.     <TextField 
  10.         ohos:id="$+id:text" 
  11.         ohos:height="50vp" 
  12.         ohos:width="319vp" 
  13.         ohos:background_element="#FFFFFF" 
  14.         ohos:hint="請(qǐng)輸入信息" 
  15.         ohos:layout_alignment="horizontal_center" 
  16.         ohos:text_alignment="center" 
  17.         ohos:text_color="#999999" 
  18.         ohos:text_size="17fp" 
  19.         ohos:top_margin="100vp"/> 
  20.  
  21.     <Button 
  22.         ohos:id="$+id:but" 
  23.         ohos:height="47vp" 
  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="24vp" 
  31.         ohos:top_margin="77vp"/> 
  32.  
  33. </DirectionalLayout> 

因?yàn)橐?onClick 方法中用到 TextField 和 Button 這兩個(gè)組件,所以要把這兩個(gè)組件移到成員位置,使其成為成員變量后,onClick 方法才能訪問(wèn)的到。

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.找到文本組件框?qū)ο?nbsp;
  23.         tf = (TextField) findComponentById(ResourceTable.Id_text); 
  24.         //找到按鈕組件對(duì)象 
  25.         but = (Button) findComponentById(ResourceTable.Id_but); 
  26.  
  27.         //2.給按鈕綁定點(diǎn)擊事件 
  28.         //當(dāng)點(diǎn)擊了按鈕之后,就要獲取文本輸入框的內(nèi)容 
  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.         //當(dāng)點(diǎn)擊了按鈕之后,獲取文本輸入框的內(nèi)容 
  46.         String message = tf.getText(); 
  47.         //利用一個(gè)Toast將信息彈出 
  48.         ToastDialog td = new ToastDialog(this); 
  49.         //大小不用設(shè)置,默認(rèn)是包裹內(nèi)容的 
  50.         //自動(dòng)關(guān)閉不用設(shè)置,默認(rèn)到了時(shí)間之后就自動(dòng)關(guān)閉 
  51.         //默認(rèn)持續(xù)時(shí)間是 2秒 
  52.  
  53.         //設(shè)置Toast的背景 
  54.         td.setTransparent(true); 
  55.         //位置(默認(rèn)居中) 
  56.         td.setAlignment(LayoutAlignment.BOTTOM); 
  57.         //設(shè)置一個(gè)偏移 
  58.         td.setOffset(0,200); 
  59.         //設(shè)置Toast內(nèi)容 
  60.         td.setText(message); 
  61.         //讓Toast出現(xiàn) 
  62.         td.show(); 
  63.     } 

運(yùn)行:

3. TextField組件高級(jí)用法

3.1 密碼的密文展示

當(dāng)輸入密碼的時(shí)候會(huì)變成密文展示。

ohos:text_input_type="pattern_password":表示輸入的密碼以密文的方式顯示。

基本使用:

  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:orientation="vertical" 
  7.     ohos:background_element="#F2F2F2"
  8.  
  9.     <TextField 
  10.         ohos:height="50vp" 
  11.         ohos:width="319vp" 
  12.         ohos:hint="請(qǐng)輸入信息" 
  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.         ohos:text_input_type="pattern_password"/> 
  20.  
  21. </DirectionalLayout> 

3.2 基線的設(shè)置

有的時(shí)候文本輸入框并不是一個(gè)框,而是下面有一條橫線,這條線華為官方叫做基線。

把文本輸入框使用橫線表示,在上面加上一條基線,把輸入框的背景顏色去掉。

  1. <TextField 
  2.         ohos:height="50vp" 
  3.         ohos:width="319vp" 
  4.         ohos:hint="請(qǐng)輸入信息" 
  5.         ohos:text_size="17fp" 
  6.         ohos:hint_color="#999999" 
  7.         ohos:text_alignment="center" 
  8.         ohos:top_margin="100vp" 
  9.         ohos:layout_alignment="horizontal_center" 
  10.         ohos:text_input_type="pattern_password" 
  11.         ohos:basement="#000000" 
  12.         /> 

如果以后看到一條基線,然后在輸入一些數(shù)字信息,這還是 TextField 文本輸入框組件,只不過(guò)是背景色沒(méi)有設(shè)置,讓它跟布局的顏色一致了,看不到背景而已。

3.3 氣泡的設(shè)置

當(dāng)用鼠標(biāo)長(zhǎng)按選中輸入的內(nèi)容后,就會(huì)選中內(nèi)容,前面的光標(biāo)和后面的光標(biāo),以及中間選中的內(nèi)容顏色會(huì)改變,華為官方給前、后的光標(biāo),以及沒(méi)有選中內(nèi)容狀態(tài)下出現(xiàn)的小氣球取名為氣泡。

  1. <TextField 
  2.         ohos:height="50vp" 
  3.         ohos:width="319vp" 
  4.         ohos:hint="請(qǐng)輸入信息" 
  5.         ohos:text_size="17fp" 
  6.         ohos:hint_color="#999999" 
  7.         ohos:text_alignment="center" 
  8.         ohos:top_margin="100vp" 
  9.         ohos:layout_alignment="horizontal_center" 
  10.         ohos:basement="#000000" 
  11.         /> 

可以設(shè)置左邊、右邊,以及沒(méi)有選中情況下的氣泡。

氣泡的圖片、顏色都是可以自定義的。

以下用到的圖片可自?。?a >https://www.aliyundrive.com/s/wT22d1Vb1BV

把左、右,以及中間沒(méi)有選中的氣泡圖片復(fù)制到 media 文件夾下。

  1. <TextField 
  2.         ohos:height="50vp" 
  3.         ohos:width="319vp" 
  4.         ohos:hint="請(qǐng)輸入信息" 
  5.         ohos:text_size="17fp" 
  6.         ohos:hint_color="#999999" 
  7.         ohos:text_alignment="center" 
  8.         ohos:top_margin="100vp" 
  9.         ohos:layout_alignment="horizontal_center" 
  10.         ohos:basement="#000000" 
  11.         ohos:element_selection_left_bubble="$media:left" 
  12.         ohos:element_selection_right_bubble="$media:right" 
  13.         ohos:element_cursor_bubble="$media:bubble" 
  14.         ohos:selection_color="#FF0000" 
  15.         /> 
  • ohos:element_selection_left_bubble、ohos:element_selection_right_bubble分別設(shè)置左右氣泡顯示的圖片
  • ohos:element_cursor_bubble:設(shè)置沒(méi)有選中時(shí)的氣泡圖片
  • ohos:selection_color:設(shè)置選中時(shí)內(nèi)容的顏色

運(yùn)行:

4. TextField案例——長(zhǎng)按查看密碼明文

在一些APP中,登錄界面密碼輸入框那里有個(gè)小眼睛,按住小眼睛后就可以看到密碼的明文展示,松開(kāi)小眼睛又恢復(fù)到密文狀態(tài)了。

把“小眼睛”改成Button組件,實(shí)現(xiàn)的邏輯原理也是一樣的。

需求分析:

  • 按住按鈕不松,將輸入框中的密碼變成明文
  • 松開(kāi)按鈕之后,輸入框中的密碼變回密文

新建項(xiàng)目:TextFieldApplication3

ability_main

  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:orientation="vertical" 
  7.     ohos:background_element="#F2F2F2" 
  8.     > 
  9.  
  10.     <TextField 
  11.         ohos:id="$+id:text" 
  12.         ohos:height="50vp" 
  13.         ohos:width="319vp" 
  14.         ohos:hint="請(qǐng)輸入密碼" 
  15.         ohos:text_size="17fp" 
  16.         ohos:hint_color="#999999" 
  17.         ohos:text_alignment="center" 
  18.         ohos:top_margin="100vp" 
  19.         ohos:layout_alignment="horizontal_center" 
  20.         ohos:background_element="#FFFFFF" 
  21.         ohos:text_input_type="pattern_password"/> 
  22.      
  23.     <Button 
  24.         ohos:id="$+id:but" 
  25.         ohos:height="47vp" 
  26.         ohos:width="319vp" 
  27.         ohos:text="查看密碼" 
  28.         ohos:text_size="24vp" 
  29.         ohos:text_color="#FEFEFE" 
  30.         ohos:text_alignment="center" 
  31.         ohos:background_element="#21a8FD" 
  32.         ohos:top_margin="77vp" 
  33.         ohos:layout_alignment="center"/> 
  34.  
  35. </DirectionalLayout> 

 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.找到兩個(gè)組件對(duì)象 
  22.         tf = (TextField) findComponentById(ResourceTable.Id_text); 
  23.         Button but = (Button) findComponentById(ResourceTable.Id_but); 
  24.  
  25.         //2.要給按鈕綁定一個(gè)觸摸事件 
  26.         //因?yàn)樵谟|摸事件中,才能獲取到按下不松或松開(kāi) 
  27.         //單擊事件——只能捕獲到點(diǎn)擊了一下 
  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.     //參數(shù)一:現(xiàn)在觸摸的按鈕 
  45.     //參數(shù)二:動(dòng)作對(duì)象 
  46.     public boolean onTouchEvent(Component component, TouchEvent touchEvent) { 
  47.         int action = touchEvent.getAction(); 
  48.  
  49.         if (action == TouchEvent.PRIMARY_POINT_DOWN){//表示按下不松的時(shí)候 
  50.             //當(dāng)按下不送的時(shí)候,將文本框中密碼變成明文 
  51.             tf.setTextInputType(InputAttribute.PATTERN_NULL); 
  52.         }else if (action == TouchEvent.PRIMARY_POINT_UP){//表示松開(kāi)的時(shí)候 
  53.             //當(dāng)松開(kāi)的時(shí)候,將文本框中的密碼變回密文 
  54.             tf.setTextInputType(InputAttribute.PATTERN_PASSWORD); 
  55.         } 
  56.         //true:表示觸摸事件的后續(xù)動(dòng)作還會(huì)進(jìn)行觸發(fā) 
  57.         //false:表示觸摸事件只觸發(fā)第一個(gè)按下不松 
  58.         return true
  59.     } 

 運(yùn)行:

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

新建項(xiàng)目:TextFieldApplication4

細(xì)節(jié)說(shuō)明:

  • Text文本(忘記密碼了?)組件默認(rèn)是左邊放置的,加上 ohos:layout_alignment="right"就是右邊放置了,同時(shí)也給個(gè)ohos:right_margin="20vp"和右邊的屏幕有些距離。如果ohos:layout_alignment="right"屬性不寫,直接寫ohos:right_margin="20vp,那么ohos:layout_alignment="right"屬性就會(huì)失效,因?yàn)榻M件默認(rèn)是放在左邊的。

ability_main

  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:orientation="vertical" 
  7.     ohos:background_element="#F2F2F2"
  8.  
  9.     <TextField 
  10.         ohos:id="$+id:username" 
  11.         ohos:height="50vp" 
  12.         ohos:width="319vp" 
  13.         ohos:hint="請(qǐng)輸入用戶名" 
  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.  
  21.     <TextField 
  22.         ohos:id="$+id:password" 
  23.         ohos:height="50vp" 
  24.         ohos:width="319vp" 
  25.         ohos:hint="請(qǐng)輸入密碼" 
  26.         ohos:text_size="17fp" 
  27.         ohos:hint_color="#999999" 
  28.         ohos:text_alignment="center" 
  29.         ohos:top_margin="10vp" 
  30.         ohos:layout_alignment="horizontal_center" 
  31.         ohos:background_element="#FFFFFF" 
  32.         ohos:text_input_type="pattern_password"/> 
  33.      
  34.     <Text 
  35.         ohos:height="match_content" 
  36.         ohos:width="match_content" 
  37.         ohos:text="忘記密碼了?" 
  38.         ohos:text_size="17fp" 
  39.         ohos:text_color="#979797" 
  40.         ohos:top_margin="13vp" 
  41.         ohos:layout_alignment="right" 
  42.         ohos:right_margin="20vp"/> 
  43.      
  44.     <Button 
  45.         ohos:height="47vp" 
  46.         ohos:width="319vp" 
  47.         ohos:text="登錄" 
  48.         ohos:text_size="24fp" 
  49.         ohos:text_color="#FEFEFE" 
  50.         ohos:text_alignment="center" 
  51.         ohos:background_element="#21a8FD" 
  52.         ohos:top_margin="77vp" 
  53.         ohos:layout_alignment="horizontal_center"/> 
  54.  
  55.     <Button 
  56.         ohos:height="47vp" 
  57.         ohos:width="319vp" 
  58.         ohos:text="注冊(cè)" 
  59.         ohos:text_size="24fp" 
  60.         ohos:text_color="#FEFEFE" 
  61.         ohos:text_alignment="center" 
  62.         ohos:background_element="#21a8FD" 
  63.         ohos:top_margin="13vp" 
  64.         ohos:layout_alignment="horizontal_center"/> 
  65.  
  66.  
  67.  
  68. </DirectionalLayout> 

 運(yùn)行:

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(qū)
相關(guān)推薦

2020-12-23 11:45:27

鴻蒙HarmonyOSTextField組件

2021-09-13 15:17:28

鴻蒙HarmonyOS應(yīng)用

2021-09-06 15:31:01

鴻蒙HarmonyOS應(yīng)用

2019-03-07 14:45:07

聊天工具富文本輸入框前端

2020-09-24 14:06:19

Vue

2021-09-27 10:43:18

鴻蒙HarmonyOS應(yīng)用

2021-07-05 14:29:28

鴻蒙HarmonyOS應(yīng)用

2023-10-20 08:02:25

圖形編輯器前端

2011-07-22 15:32:53

iPhone 按鈕 對(duì)話框

2021-07-13 09:49:08

鴻蒙HarmonyOS應(yīng)用

2024-06-13 15:43:04

2022-04-06 18:29:58

CSSJS輸入框

2017-08-14 12:45:54

Windows 10Windows開(kāi)機(jī)密碼

2021-11-01 11:08:28

鴻蒙HarmonyOS應(yīng)用

2024-06-11 00:00:20

Input自動(dòng)拼寫開(kāi)發(fā)

2021-12-13 16:44:49

鴻蒙HarmonyOS應(yīng)用

2022-04-11 11:07:37

HarmonyUI小型系統(tǒng)textarea

2017-09-11 17:46:48

設(shè)計(jì)

2011-08-12 09:35:05

javascript

2012-06-29 14:13:10

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)