【鸿蒙 HarmonyOS】UI 组件 ( 文本输入框 TextField 组件 )
生活随笔
收集整理的這篇文章主要介紹了
【鸿蒙 HarmonyOS】UI 组件 ( 文本输入框 TextField 组件 )
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、布局中設置 TextField 組件
- 二、代碼中獲取并設置 TextField 組件
一、布局中設置 TextField 組件
TextField 組件是文本輸入框 , 允許用戶在界面中輸入指定的文字信息 ;
布局文件中配置 TextField 組件 :
<?xml version="1.0" encoding="utf-8"?> <DirectionalLayoutxmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"><!-- 輸入框組件 --><TextFieldohos:id="$+id:textfield"ohos:height="match_content"ohos:width="match_parent"ohos:layout_alignment="horizontal_center"ohos:text="Hello World"ohos:text_size="50"ohos:text_input_type="pattern_number"/></DirectionalLayout>其中 ohos:text_input_type=“pattern_number” 是配置輸入的類型 , 這里是輸入數字 ;
顯示效果 : 剛進入界面時效果與 Text 組件相同 , 但是點擊后 , 會顯示光標 , 并彈出輸入鍵盤 ;
剛進入界面時 :
點擊后效果 : 彈出軟鍵盤 ;
二、代碼中獲取并設置 TextField 組件
簡單展示一下獲取組件 , 并設置其顯示文本 ;
package com.example.textfield.slice;import com.example.textfield.ResourceTable; import ohos.aafwk.ability.AbilitySlice; import ohos.aafwk.content.Intent; import ohos.agp.components.TextField;public class MainAbilitySlice extends AbilitySlice {@Overridepublic void onStart(Intent intent) {super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);// 獲取布局文件中的 TextField 組件TextField textField = (TextField) findComponentById(ResourceTable.Id_textfield);// 設置默認顯示的內容textField.setText("請輸入手機號 : ");}@Overridepublic void onActive() {super.onActive();}@Overridepublic void onForeground(Intent intent) {super.onForeground(intent);} }運行效果 :
總結
以上是生活随笔為你收集整理的【鸿蒙 HarmonyOS】UI 组件 ( 文本输入框 TextField 组件 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【鸿蒙 HarmonyOS】UI 组件
- 下一篇: 【鸿蒙 HarmonyOS】UI 组件