HarmonyOS實戰——TextField文字輸入框元件基本使用
阿新 • • 發佈:2021-09-28
HarmonyOS實戰——TextField文字輸入框元件基本使用
- 【鴻蒙專欄,從入門到實戰系列】:
https://blog.csdn.net/qq_41684621/category_10128500.html
- 1. TextField元件基本用法
- 2. TextField案例——獲取文字輸入框中的內容並進行Toast提示
- 3. TextField元件高階用法
- 4. TextField案例——長按檢視密碼明文
- 5. TextField案例——搭建登入介面
1. TextField元件基本用法
元件說明:
-
是
Text
的子類,用來進行使用者輸入資料的
常見屬性:
2. TextField案例——獲取文字輸入框中的內容並進行Toast提示
-
通過
TextField
獲取文字輸入框中的內容並進行Toast
提示 -
新建專案:
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="請輸入資訊" 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>
- 因為要在
onClick
方法中用到TextField
和Button
這兩個元件,所以要把這兩個元件移到成員位置,使其成為成員變數後,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.找到文字元件框物件 tf = (TextField) findComponentById(ResourceTable.Id_text); //找到按鈕元件物件 but = (Button) findComponentById(ResourceTable.Id_but); //2.給按鈕繫結點選事件 //當點選了按鈕之後,就要獲取文字輸入框的內容 but.setClickedListener(this); } @Override public void onActive() { super.onActive(); } @Override public void onForeground(Intent intent) { super.onForeground(intent); } @Override public void onClick(Component component) { //當點選了按鈕之後,獲取文字輸入框的內容 String message = tf.getText(); //利用一個Toast將資訊彈出 ToastDialog td = new ToastDialog(this); //大小不用設定,預設是包裹內容的 //自動關閉不用設定,預設到了時間之後就自動關閉 //預設持續時間是 2秒 //設定Toast的背景 td.setTransparent(true); //位置(預設居中) td.setAlignment(LayoutAlignment.BOTTOM); //設定一個偏移 td.setOffset(0,200); //設定Toast內容 td.setText(message); //讓Toast出現 td.show(); } }
- 執行:
3. TextField元件高階用法
3.1 密碼的密文展示
- 當輸入密碼的時候會變成密文展示
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="請輸入資訊"
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 基線的設定
-
有的時候文字輸入框並不是一個框,而是下面有一條橫線,這條線華為官方叫做 基線
-
把文字輸入框使用橫線表示,在上面加上一條基線,把輸入框的背景顏色去掉
<TextField
ohos:height="50vp"
ohos:width="319vp"
ohos:hint="請輸入資訊"
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"
/>
- 如果以後看到一條基線,然後在輸入一些數字資訊,這還是
TextField
文字輸入框元件,只不過是背景色沒有設定,讓它跟佈局的顏色一致了,看不到背景而已
3.3 氣泡的設定
- 當用滑鼠長按選中輸入的內容後,就會選中內容,前面的游標和後面的游標,以及中間選中的內容顏色會改變,華為官方給前、後的游標,以及沒有選中內容狀態下出現的小氣球取名為氣泡
<TextField
ohos:height="50vp"
ohos:width="319vp"
ohos:hint="請輸入資訊"
ohos:text_size="17fp"
ohos:hint_color="#999999"
ohos:text_alignment="center"
ohos:top_margin="100vp"
ohos:layout_alignment="horizontal_center"
ohos:basement="#000000"
/>
- 可以設定左邊、右邊,以及沒有選中情況下的氣泡
- 氣泡的圖片、顏色都是可以自定義的
- 以下用到的圖片可自取:https://www.aliyundrive.com/s/wT22d1Vb1BV
- 把左、右,以及中間沒有選中的氣泡圖片複製到
media
資料夾下
<TextField
ohos:height="50vp"
ohos:width="319vp"
ohos:hint="請輸入資訊"
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
分別設定左右氣泡顯示的圖片ohos:element_cursor_bubble
:設定沒有選中時的氣泡圖片ohos:selection_color
:設定選中時內容的顏色- 執行:
4. TextField案例——長按檢視密碼明文
在一些APP中,登入介面密碼輸入框那裡有個小眼睛,按住小眼睛後就可以看到密碼的明文展示,鬆開小眼睛又恢復到密文狀態了
- 把“小眼睛”改成Button元件,實現的邏輯原理也是一樣的
需求分析:
- 按住按鈕不鬆,將輸入框中的密碼變成明文
- 鬆開按鈕之後,輸入框中的密碼變回密文
新建專案: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="請輸入密碼"
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.找到兩個元件物件
tf = (TextField) findComponentById(ResourceTable.Id_text);
Button but = (Button) findComponentById(ResourceTable.Id_but);
//2.要給按鈕繫結一個觸控事件
//因為在觸控事件中,才能獲取到按下不鬆或鬆開
//單擊事件——只能捕獲到點選了一下
but.setTouchEventListener(this);
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
@Override
//引數一:現在觸控的按鈕
//引數二:動作物件
public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
int action = touchEvent.getAction();
if (action == TouchEvent.PRIMARY_POINT_DOWN){//表示按下不鬆的時候
//當按下不送的時候,將文字框中密碼變成明文
tf.setTextInputType(InputAttribute.PATTERN_NULL);
}else if (action == TouchEvent.PRIMARY_POINT_UP){//表示鬆開的時候
//當鬆開的時候,將文字框中的密碼變回密文
tf.setTextInputType(InputAttribute.PATTERN_PASSWORD);
}
//true:表示觸控事件的後續動作還會進行觸發
//false:表示觸控事件只觸發第一個按下不鬆
return true;
}
}
- 執行:
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
<?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="請輸入使用者名稱"
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="請輸入密碼"
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="註冊"
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>
- 執行:
本文來自部落格園,作者:兮動人,轉載請註明原文連結:https://www.cnblogs.com/xdr630/p/15350319.html