0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

HarmonyOS系統(tǒng)TextField組件基本用法

電子發(fā)燒友論壇 ? 來源:電子發(fā)燒友論壇 ? 作者:兮動(dòng)人 ? 2021-10-09 09:18 ? 次閱讀

1. TextField組件基本用法

組件說明:

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

常見屬性:

5e2291ba-2381-11ec-82a8-dac502259ad0.png

《TextField ohos:id=“$+id:text” ohos:height=“50vp” ohos:width=“319vp” ohos:background_element=“#FFFFFF” ohos:hint=“請(qǐng)輸入信息” ohos:layout_alignment=“horizontal_center” ohos:text_alignment=“center” ohos:text_color=“#999999” ohos:text_size=“17fp” ohos:top_margin=“100vp”/》

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

通過TextField獲取文本輸入框中的內(nèi)容并進(jìn)行Toast提示

新建項(xiàng)目:TextFieldApplication

ability_main

《?xml version=“1.0” encoding=“utf-8”?》《DirectionalLayout xmlns:ohos=“http://schemas.huawei.com/res/ohos” ohos:height=“match_parent” ohos:width=“match_parent” ohos:background_element=“#F2F2F2” ohos:orientation=“vertical”》

《TextField ohos:id=“$+id:text” ohos:height=“50vp” ohos:width=“319vp” ohos:background_element=“#FFFFFF” ohos:hint=“請(qǐng)輸入信息” ohos:layout_alignment=“horizontal_center” ohos:text_alignment=“center” ohos:text_color=“#999999” ohos:text_size=“17fp” ohos:top_margin=“100vp”/》

《Button ohos:id=“$+id:but” ohos:height=“47vp” ohos:width=“319vp” ohos:background_element=“#21a8FD” ohos:layout_alignment=“center” ohos:text=“獲取信息” ohos:text_alignment=“center” ohos:text_color=“#FEFEFE” ohos:text_size=“24vp” ohos:top_margin=“77vp”/》

《/DirectionalLayout》

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

MainAbilitySlice

package com.xdr630.textfieldapplication.slice;

import com.xdr630.textfieldapplication.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Button;import ohos.agp.components.Component;import ohos.agp.components.TextField;import ohos.agp.utils.LayoutAlignment;import ohos.agp.window.dialog.ToastDialog;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

TextField tf; Button but;

@Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main);

//1.找到文本組件框?qū)ο?tf = (TextField) findComponentById(ResourceTable.Id_text); //找到按鈕組件對(duì)象 but = (Button) findComponentById(ResourceTable.Id_but);

//2.給按鈕綁定點(diǎn)擊事件 //當(dāng)點(diǎn)擊了按鈕之后,就要獲取文本輸入框的內(nèi)容 but.setClickedListener(this);

}

@Override public void onActive() { super.onActive(); }

@Override public void onForeground(Intent intent) { super.onForeground(intent); }

@Override public void onClick(Component component) { //當(dāng)點(diǎn)擊了按鈕之后,獲取文本輸入框的內(nèi)容 String message = tf.getText(); //利用一個(gè)Toast將信息彈出 ToastDialog td = new ToastDialog(this); //大小不用設(shè)置,默認(rèn)是包裹內(nèi)容的 //自動(dòng)關(guān)閉不用設(shè)置,默認(rèn)到了時(shí)間之后就自動(dòng)關(guān)閉 //默認(rèn)持續(xù)時(shí)間是 2秒

//設(shè)置Toast的背景 td.setTransparent(true); //位置(默認(rèn)居中) td.setAlignment(LayoutAlignment.BOTTOM); //設(shè)置一個(gè)偏移 td.setOffset(0,200); //設(shè)置Toast內(nèi)容 td.setText(message); //讓Toast出現(xiàn) td.show(); }}

運(yùn)行:

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

3.1 密碼的密文展示

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

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

基本使用:

《?xml version=“1.0” encoding=“utf-8”?》《DirectionalLayout xmlns:ohos=“http://schemas.huawei.com/res/ohos” ohos:height=“match_parent” ohos:width=“match_parent” ohos:orientation=“vertical” ohos:background_element=“#F2F2F2”》

《TextField ohos:height=“50vp” ohos:width=“319vp” ohos:hint=“請(qǐng)輸入信息” ohos:text_size=“17fp” ohos:hint_color=“#999999” ohos:text_alignment=“center” ohos:top_margin=“100vp” ohos:layout_alignment=“horizontal_center” ohos:background_element=“#FFFFFF” ohos:text_input_type=“pattern_password”/》

《/DirectionalLayout》

3.2 基線的設(shè)置

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

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

《TextField ohos:height=“50vp” ohos:width=“319vp” ohos:hint=“請(qǐng)輸入信息” ohos:text_size=“17fp” ohos:hint_color=“#999999” ohos:text_alignment=“center” ohos:top_margin=“100vp” ohos:layout_alignment=“horizontal_center” ohos:text_input_type=“pattern_password” ohos:basement=“#000000” /》

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

3.3 氣泡的設(shè)置

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

《TextField ohos:height=“50vp” ohos:width=“319vp” ohos:hint=“請(qǐng)輸入信息” ohos:text_size=“17fp” ohos:hint_color=“#999999” ohos:text_alignment=“center” ohos:top_margin=“100vp” ohos:layout_alignment=“horizontal_center” ohos:basement=“#000000” /》

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

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

以下用到的圖片可自?。?/p>

https://www.aliyundrive.com/s/wT22d1Vb1BV

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

《TextField ohos:height=“50vp” ohos:width=“319vp” ohos:hint=“請(qǐng)輸入信息” ohos:text_size=“17fp” ohos:hint_color=“#999999” ohos:text_alignment=“center” ohos:top_margin=“100vp” ohos:layout_alignment=“horizontal_center” ohos:basement=“#000000” ohos:element_selection_left_bubble=“$media:left” ohos:element_selection_right_bubble=“$media:right” ohos:element_cursor_bubble=“$media:bubble” ohos:selection_color=“#FF0000” /》

ohos:element_selection_left_bubble、ohos:element_selection_right_bubble分別設(shè)置左右氣泡顯示的圖片

ohos:element_cursor_bubble:設(shè)置沒有選中時(shí)的氣泡圖片

ohos:selection_color:設(shè)置選中時(shí)內(nèi)容的顏色

運(yùn)行:

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

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

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

需求分析:

按住按鈕不松,將輸入框中的密碼變成明文

松開按鈕之后,輸入框中的密碼變回密文

新建項(xiàng)目:TextFieldApplication3

ability_main

《?xml version=“1.0” encoding=“utf-8”?》《DirectionalLayout xmlns:ohos=“http://schemas.huawei.com/res/ohos” ohos:height=“match_parent” ohos:width=“match_parent” ohos:orientation=“vertical” ohos:background_element=“#F2F2F2” 》

《TextField ohos:id=“$+id:text” ohos:height=“50vp” ohos:width=“319vp” ohos:hint=“請(qǐng)輸入密碼” ohos:text_size=“17fp” ohos:hint_color=“#999999” ohos:text_alignment=“center” ohos:top_margin=“100vp” ohos:layout_alignment=“horizontal_center” ohos:background_element=“#FFFFFF” ohos:text_input_type=“pattern_password”/》 《Button ohos:id=“$+id:but” ohos:height=“47vp” ohos:width=“319vp” ohos:text=“查看密碼” ohos:text_size=“24vp” ohos:text_color=“#FEFEFE” ohos:text_alignment=“center” ohos:background_element=“#21a8FD” ohos:top_margin=“77vp” ohos:layout_alignment=“center”/》

《/DirectionalLayout》

MainAbilitySlice

package com.xdr630.textfieldapplication3.slice;

import com.xdr630.textfieldapplication3.ResourceTable;import ohos.aafwk.ability.AbilitySlice;import ohos.aafwk.content.Intent;import ohos.agp.components.Button;import ohos.agp.components.Component;import ohos.agp.components.InputAttribute;import ohos.agp.components.TextField;import ohos.multimodalinput.event.TouchEvent;

public class MainAbilitySlice extends AbilitySlice implements Component.TouchEventListener {

TextField tf;

@Override public void onStart(Intent intent) { super.onStart(intent); super.setUIContent(ResourceTable.Layout_ability_main);

//1.找到兩個(gè)組件對(duì)象 tf = (TextField) findComponentById(ResourceTable.Id_text); Button but = (Button) findComponentById(ResourceTable.Id_but);

//2.要給按鈕綁定一個(gè)觸摸事件 //因?yàn)樵谟|摸事件中,才能獲取到按下不松或松開 //單擊事件——只能捕獲到點(diǎn)擊了一下 but.setTouchEventListener(this);

}

@Override public void onActive() { super.onActive(); }

@Override public void onForeground(Intent intent) { super.onForeground(intent); }

@Override //參數(shù)一:現(xiàn)在觸摸的按鈕 //參數(shù)二:動(dòng)作對(duì)象 public boolean onTouchEvent(Component component, TouchEvent touchEvent) { int action = touchEvent.getAction();

if (action == TouchEvent.PRIMARY_POINT_DOWN){//表示按下不松的時(shí)候 //當(dāng)按下不送的時(shí)候,將文本框中密碼變成明文 tf.setTextInputType(InputAttribute.PATTERN_NULL); }else if (action == TouchEvent.PRIMARY_POINT_UP){//表示松開的時(shí)候 //當(dāng)松開的時(shí)候,將文本框中的密碼變回密文 tf.setTextInputType(InputAttribute.PATTERN_PASSWORD); } //true:表示觸摸事件的后續(xù)動(dòng)作還會(huì)進(jìn)行觸發(fā) //false:表示觸摸事件只觸發(fā)第一個(gè)按下不松 return true; }}

運(yùn)行:

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

新建項(xiàng)目:TextFieldApplication4

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

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

《?xml version=”1.0“ encoding=”utf-8“?》《DirectionalLayout xmlns:ohos=”http://schemas.huawei.com/res/ohos“ ohos:height=”match_parent“ ohos:width=”match_parent“ ohos:orientation=”vertical“ ohos:background_element=”#F2F2F2“》

《TextField ohos:id=”$+id:username“ ohos:height=”50vp“ ohos:width=”319vp“ ohos:hint=”請(qǐng)輸入用戶名“ ohos:text_size=”17fp“ ohos:hint_color=”#999999“ ohos:text_alignment=”center“ ohos:top_margin=”100vp“ ohos:layout_alignment=”horizontal_center“ ohos:background_element=”#FFFFFF“/》

《TextField ohos:id=”$+id:password“ ohos:height=”50vp“ ohos:width=”319vp“ ohos:hint=”請(qǐng)輸入密碼“ ohos:text_size=”17fp“ ohos:hint_color=”#999999“ ohos:text_alignment=”center“ ohos:top_margin=”10vp“ ohos:layout_alignment=”horizontal_center“ ohos:background_element=”#FFFFFF“ ohos:text_input_type=”pattern_password“/》 《Text ohos:height=”match_content“ ohos:width=”match_content“ ohos:text=”忘記密碼了?“ ohos:text_size=”17fp“ ohos:text_color=”#979797“ ohos:top_margin=”13vp“ ohos:layout_alignment=”right“ ohos:right_margin=”20vp“/》 《Button ohos:height=”47vp“ ohos:width=”319vp“ ohos:text=”登錄“ ohos:text_size=”24fp“ ohos:text_color=”#FEFEFE“ ohos:text_alignment=”center“ ohos:background_element=”#21a8FD“ ohos:top_margin=”77vp“ ohos:layout_alignment=”horizontal_center“/》

《Button ohos:height=”47vp“ ohos:width=”319vp“ ohos:text=”注冊(cè)“ ohos:text_size=”24fp“ ohos:text_color=”#FEFEFE“ ohos:text_alignment=”center“ ohos:background_element=”#21a8FD“ ohos:top_margin=”13vp“ ohos:layout_alignment=”horizontal_center“/》

《/DirectionalLayout》

責(zé)任編輯:haq

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 操作系統(tǒng)
    +關(guān)注

    關(guān)注

    37

    文章

    6620

    瀏覽量

    123046
  • 文本
    +關(guān)注

    關(guān)注

    0

    文章

    118

    瀏覽量

    17047
  • HarmonyOS
    +關(guān)注

    關(guān)注

    79

    文章

    1958

    瀏覽量

    29916

原文標(biāo)題:【鴻蒙實(shí)戰(zhàn)】HarmonyOS實(shí)戰(zhàn)——TextField文本輸入框組件基本使用

文章出處:【微信號(hào):gh_9b9470648b3c,微信公眾號(hào):電子發(fā)燒友論壇】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏

    評(píng)論

    相關(guān)推薦

    HarmonyOS NEXT Developer Beta1最新術(shù)語(yǔ)表

    HAP中。 ANS Advanced Notification Service,通知增強(qiáng)服務(wù),是HarmonyOS中負(fù)責(zé)處理通知的訂閱、發(fā)布和更新等操作的系統(tǒng)服務(wù)。 Atomic Service,元
    發(fā)表于 06-27 16:16

    鴻蒙Ability Kit(程序框架服務(wù))【UIAbility組件基本用法

    UIAbility組件的基本用法包括:指定UIAbility的啟動(dòng)頁(yè)面以及獲取UIAbility的上下文[UIAbilityContext]。
    的頭像 發(fā)表于 06-06 11:02 ?373次閱讀
    鴻蒙Ability Kit(程序框架服務(wù))【UIAbility<b class='flag-5'>組件</b>基本<b class='flag-5'>用法</b>】

    HarmonyOS開發(fā)案例:【使用List組件實(shí)現(xiàn)設(shè)置項(xiàng)】

    使用List組件、Toggle組件以及Router接口,實(shí)現(xiàn)一個(gè)簡(jiǎn)單的設(shè)置頁(yè),點(diǎn)擊將跳轉(zhuǎn)到對(duì)應(yīng)的詳細(xì)設(shè)置頁(yè)面。
    的頭像 發(fā)表于 05-10 17:01 ?818次閱讀
    <b class='flag-5'>HarmonyOS</b>開發(fā)案例:【使用List<b class='flag-5'>組件</b>實(shí)現(xiàn)設(shè)置項(xiàng)】

    HarmonyOS開發(fā)案例:【使用List組件實(shí)現(xiàn)商品列表】

    OpenHarmony ArkTS提供了豐富的接口和組件,開發(fā)者可以根據(jù)實(shí)際場(chǎng)景和開發(fā)需求,選用不同的組件和接口。
    的頭像 發(fā)表于 05-10 16:41 ?1192次閱讀
    <b class='flag-5'>HarmonyOS</b>開發(fā)案例:【使用List<b class='flag-5'>組件</b>實(shí)現(xiàn)商品列表】

    HarmonyOS開發(fā)案例:【基礎(chǔ)組件Slider的使用】

    學(xué)習(xí)如何使用聲明式UI編程框架的基礎(chǔ)組件。本篇Codelab將會(huì)使用Image組件、Slider組件、Text組件共同實(shí)現(xiàn)一個(gè)可調(diào)節(jié)的風(fēng)車動(dòng)畫
    的頭像 發(fā)表于 05-10 16:01 ?617次閱讀
    <b class='flag-5'>HarmonyOS</b>開發(fā)案例:【基礎(chǔ)<b class='flag-5'>組件</b>Slider的使用】

    HarmonyOS開發(fā)案例:【W(wǎng)eb組件實(shí)現(xiàn)抽獎(jiǎng)】

    基于ArkTS的聲明式開發(fā)范式的樣例,主要介紹了Web組件如何加載本地和云端H5小程序。
    的頭像 發(fā)表于 05-09 18:31 ?1270次閱讀
    <b class='flag-5'>HarmonyOS</b>開發(fā)案例:【W(wǎng)eb<b class='flag-5'>組件</b>實(shí)現(xiàn)抽獎(jiǎng)】

    HarmonyOS開發(fā)案例:【常用組件與布局】

    HarmonyOS ArkUI提供了豐富多樣的UI組件,您可以使用這些組件輕松地編寫出更加豐富、漂亮的界面。
    的頭像 發(fā)表于 05-09 18:20 ?1104次閱讀
    <b class='flag-5'>HarmonyOS</b>開發(fā)案例:【常用<b class='flag-5'>組件</b>與布局】

    HarmonyOS開發(fā)案例:【 slider組件的使用】

    主要介紹slider滑動(dòng)條組件的使用。如圖所示拖動(dòng)對(duì)應(yīng)滑動(dòng)條調(diào)節(jié)風(fēng)車的旋轉(zhuǎn)速度以及縮放比例。
    的頭像 發(fā)表于 04-25 22:02 ?827次閱讀
    <b class='flag-5'>HarmonyOS</b>開發(fā)案例:【 slider<b class='flag-5'>組件</b>的使用】

    HarmonyOS開發(fā)案例:【 switch、chart組件的使用】

    基于switch組件和chart組件,實(shí)現(xiàn)線形圖、占比圖、柱狀圖,并通過switch切換chart組件數(shù)據(jù)的動(dòng)靜態(tài)顯示。
    的頭像 發(fā)表于 04-25 20:58 ?482次閱讀
    <b class='flag-5'>HarmonyOS</b>開發(fā)案例:【 switch、chart<b class='flag-5'>組件</b>的使用】

    鴻蒙OS開發(fā)實(shí)例:【組件化模式】

    組件化一直是移動(dòng)端比較流行的開發(fā)方式,有著編譯運(yùn)行快,業(yè)務(wù)邏輯分明,任務(wù)劃分清晰等優(yōu)點(diǎn),針對(duì)Android端的組件化;與Android端的組件化相比,HarmonyOS
    的頭像 發(fā)表于 04-07 17:44 ?531次閱讀
    鴻蒙OS開發(fā)實(shí)例:【<b class='flag-5'>組件</b>化模式】

    2024款鴻蒙OS 最新HarmonyOS Next_HarmonyOS4.0系列教程分享

    鴻蒙的出現(xiàn),標(biāo)志著中國(guó)科技的崛起。HarmonyOS就是我們說的華為鴻蒙系統(tǒng),截止到2023年8月4日已有超過7億臺(tái)設(shè)備搭載了鴻蒙OS系統(tǒng)。據(jù)多家媒體報(bào)道,2024年國(guó)內(nèi)有21所985大學(xué)都開設(shè)
    發(fā)表于 02-28 10:29

    HarmonyOS開發(fā)技術(shù)全面分析

    HarmonyOS 通過組件化和小型化等設(shè)計(jì)方法,支持多種終端設(shè)備按需彈性部署,能夠適配不同類別的硬件資源和功能需求。支撐通過編譯鏈關(guān)系去自動(dòng)生成組件化的依賴關(guān)系,形成組件樹依賴圖,
    發(fā)表于 02-21 16:31

    了解鴻蒙OS Text組件

    文本(Text)是用來顯示字符串的組件,在界面上顯示為一塊文本區(qū)域。Text 作為一個(gè)基本組件,有很多擴(kuò)展,常見的有按鈕組件 Button,文本編輯組件
    的頭像 發(fā)表于 01-29 15:24 ?1125次閱讀
    了解鴻蒙OS Text<b class='flag-5'>組件</b>

    harmonyos和安卓的區(qū)別

    HarmonyOS是由華為開發(fā)的一款全場(chǎng)景分布式操作系統(tǒng),而安卓(Android)則是由谷歌開發(fā)的移動(dòng)操作系統(tǒng)。雖然兩者都是操作系統(tǒng),但它們?cè)诤芏喾矫娲嬖诿黠@的區(qū)別。下面是關(guān)于
    的頭像 發(fā)表于 01-10 17:55 ?3177次閱讀

    pcb插針的用法

    pcb插針的用法
    的頭像 發(fā)表于 12-13 16:58 ?1648次閱讀