字段是什么意思(鸿蒙系统应用程序开发)
admin
2023-09-29 08:02:39
0

鸿蒙App开发之TextField

经过前面对文字和按钮的解释,我们实现了一个拨号界面。但实际上拨号号码显示并没有使用文本组件,因为删除起来太麻烦了。

拨号界面的真正组件是TextField文本框。同时,它也继承自文本组件:

publiclclasstextfield extends text与上一章相同。今天我们将解释TextField文本框的用法。

创建TextField

首先,我们仍然使用XML布局文件来创建TextField组件。示例代码如下:

textfield ohos : height=' 40vp ' ohos : width=' match _ parent ' ohos : text _ size=' 25vp ' ohos : margin=' 20vp ' ohos : padding=' 5vp ' ohos : background _ El Element=' $ graphic 3: background _ ability _ main ' ohos 3360 hint='请输入用户名' ohos 3360 basage=' # 000

这里有几个重要的数据需要我们注意,如下表所示:

属性

意义

暗示

文本框提示内容

地下室

输入框的基线是图中的绿色直线。

文本对齐

输入垂直居中。

元素_光标_气泡

文字的光标气泡就是图中的绿色圆圈。

多行

多行显示

文本输入类型

例如,如果密码设置为pattern_password,则只显示* * *符号。

我们没有在上面的代码中设置这个属性。接下来,我们把它改成长方形的红色。示例代码如下:

ohos : element _ cursor _ bubble=' $ graphic : textfield_bubble '这里我们只需要添加一行属性,图形文件夹下textfield _ bubble的内容如下:

?xmlversion='1.0 '编码='UTF-8 'shape xmlns : hos=' http://schemas.huawei.com/res/ohos' ohos : shape='矩形' solidohos 3360 color=' # ff 0000 '/shape,效果如下:

从010到1010,我们已经学习了三个组件:文本、按钮和文本字段。

通过这三个组件,我们完全可以在手机上实现登录界面。下面,我们来实现登录界面的效果。

首先是我们的XML布局文件,代码如下:

?xmlversion='1.0 '编码='utf-8 'directional layout xmlns : ohos=' http://schemas . Huawei.com/RES/ohos ' ohos : height=' match _ parent ' ohos : width=' match _ parent ' ohos : alignment=' center ' ohos : background _ element=' $ media : background ' ohos : orientation=' vertical ' textohos 3360 height=' match _ content ' ohos :

rgin="20vp"ohos:text="登录界面"/>

Java页面代码:

packagecom.liyuanjing.idacommunity.slice;importcom.liyuanjing.idacommunity.ResourceTable;importohos.aafwk.ability.AbilitySlice;importohos.aafwk.content.Intent;importohos.agp.components.Button;importohos.agp.components.Component;importohos.agp.components.Text;importohos.agp.components.TextField;importohos.agp.components.element.ShapeElement;publicclassMainAbilitySliceextendsAbilitySlice{privateTextFieldusername_textField;privateTextFieldpassword_textField;privateButtonlogin_button;privateTextusername_text;privateTextpassword_text;@OverridepublicvoidonStart(Intentintent){super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);//设置输入框不被软键盘遮挡getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN);this.username_textField=(TextField)findComponentById(ResourceTable.Id_textfield_username);this.password_textField=(TextField)findComponentById(ResourceTable.Id_textfield_password);this.login_button=(Button)findComponentById(ResourceTable.Id_button_login);this.username_text=(Text)findComponentById(ResourceTable.Id_text_username);this.password_text=(Text)findComponentById(ResourceTable.Id_text_password);this.login_button.setClickedListener(newComponent.ClickedListener(){@OverridepublicvoidonClick(Componentcomponent){username_text.setVisibility(Component.VISIBLE);password_text.setVisibility(Component.VISIBLE);//改变TextField错误样式ShapeElementerrorElement=newShapeElement(MainAbilitySlice.this,ResourceTable.Graphic_textfield_error);username_textField.setBackground(errorElement);password_textField.setBackground(errorElement);}});this.username_textField.setFocusChangedListener(newComponent.FocusChangedListener(){@OverridepublicvoidonFocusChange(Componentcomponent,booleanb){if(b){//获取到焦点if(username_text.getVisibility()==Component.VISIBLE){username_text.setVisibility(Component.INVISIBLE);}}else{//失去焦点ShapeElementbackgroundElement=newShapeElement(MainAbilitySlice.this,ResourceTable.Graphic_background_ability_main);username_textField.setBackground(backgroundElement);}}});this.password_textField.setFocusChangedListener(newComponent.FocusChangedListener(){@OverridepublicvoidonFocusChange(Componentcomponent,booleanb){if(b){//获取到焦点if(password_text.getVisibility()==Component.VISIBLE){password_text.setVisibility(Component.INVISIBLE);}}else{//失去焦点ShapeElementbackgroundElement=newShapeElement(MainAbilitySlice.this,ResourceTable.Graphic_background_ability_main);password_textField.setBackground(backgroundElement);}}});}@OverridepublicvoidonActive(){super.onActive();}@OverridepublicvoidonForeground(Intentintent){super.onForeground(intent);}}

默认TextField样式(background_ability_main.xml):

点击登录后TextField样式(textfield_error.xml):

这里需要注意的是,通过Java改变TextField的代码为:

ShapeElementbackgroundElement=newShapeElement(MainAbilitySlice.this,ResourceTable.Graphic_background_ability_main);username_textField.setBackground(backgroundElement);

同样的,TextField获取焦点事件以及失去焦点事件由FocusChangedListener进行实现,代码如下:

this.password_textField.setFocusChangedListener(newComponent.FocusChangedListener(){@OverridepublicvoidonFocusChange(Componentcomponent,booleanb){if(b){//获取到焦点}else{//失去焦点}}});

最终实现的效果如首图所示。

相关内容

热门资讯

玩家必看“新火神大厅到底有没有... 您好:新火神大厅这款游戏可以开挂,确实是有挂的,需要软件加微信【8700483】,很多玩家在新火神大...
玩家必看“新猴王牛牛透视软件辅... 您好:新猴王牛牛这款游戏可以开挂,确实是有挂的,需要软件加微信【8700483】,很多玩家在新猴王牛...
科普实测“新道游牛牛究竟有透视... 您好:新道游牛牛这款游戏可以开挂,确实是有挂的,需要软件加微信【8700483】,很多玩家在新道游牛...
重大通报“欢乐游到底能不能开透... 您好:欢乐游这款游戏可以开挂,确实是有挂的,需要了解加客服微信【8700483】很多玩家在欢乐游这款...
今日重大通报“新蜜瓜斗牛到底真... 您好:新蜜瓜斗牛这款游戏可以开挂,确实是有挂的,需要软件加微信【8700483】,很多玩家在新蜜瓜斗...