赞
踩
TextField文本输入框组件是Text的子类,用来进行用户输入数据的。
文本输入框是交互类组件
常见属性:
属性名称 | 功能说明 |
---|---|
hint | 提示文字 |
basement | 输入框基线的颜色 |
element_cursor_bubble | 设置提示气泡 |
selection_color | 选中文字的颜色 |
element_selection_left_bubble | 设置选中之后左边的气泡 |
element_selection_right_bubble | 设置选中之后右边的气泡 |
text_input_type | 输入框中的输入类型(pattern_password密文展示) |
ability_main.xml代码:
<?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:hint_color="#999999"
ohos:layout_alignment="horizontal_center"
ohos:text_alignment="center"
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="24fp"
ohos:top_margin="77vp"
/>
</DirectionalLayout>
MainAbilitySlice.java代码:
package com.example.textfieldapplication.slice;
import com.example.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();
//利用吐司弹出信息
ToastDialog td = new ToastDialog(this);
//大小不用设置,默认包裹内容
//自动关闭不用设置,默认到时间自动关闭
//持续时间默认两秒钟
//设置吐司背景:透明
td.setTransparent(true);
//位置(默认居中)
td.setAlignment(LayoutAlignment.BOTTOM);
//设置偏移
td.setOffset(0,200);
//设置内容
td.setText(message);
//吐司显示
td.show();
}
}
实现内容:点击获取信息按钮弹出吐司弹框,显示文本框中的内容
效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。