AI开发实战4-文本输入框(TextBox)的定制1
2017-08-22 21:23
309 查看
4 文本输入框(TextBox)的定制
4.1增加公共的属性和函数
文本输入框也是常使用的组件,App Inventor2提供的组件已经包含了许多属性和函数,但有些常用的属性并未提供。如用户在文本框中输入字符的时候,常会有字符个数限制的,现就开发一个设置字符串长度的属性,以方便使用。
App Inventor2默认提供了两种文本输入框:密码输入框和普通的文本输入框,在源码中对应的类分别是:PasswordTextBox和TextBox,两者有共同的基类:TextBoxBase,在TextBase.java中定义了输入框的属性和函数,现就在此文件中增加设置字符串长度的属性和相关函数。
在TextBase.java中的修改如下:
//增加表示字符串长度的变量
private int textLength;
/** 增加获取字符串长度的函数
* Returns the length of thetextbox's text
*/
@SimpleProperty(
category = PropertyCategory.BEHAVIOR,
description = "Set the length oftext")
public int TextLength() {
return textLength;
}
/**增加设置字符串长度的函数,默认值为0
* Specifies the length of thetextbox's text
* @param length
*/
@DesignerProperty(editorType =PropertyTypeConstants.PROPERTY_TYPE_NON_NEGATIVE_INTEGER,
defaultValue = "0")
@SimpleProperty
public void TextLength(int length) {
this.textLength = length;
}
@SimpleProperty是App Inventor使用的注解,表示定义了一个属性,源码如下:
/**
*Annotation to mark Simple properties.
*
*<p>Both the getter and the setter method of the property need to bemarked
*with this annotation.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SimpleProperty {
/**
*If non-empty, description to use in user-level documentation.
*/
Stringdescription() default "";
/**
* Category of property for user-level documentation. This only needs
* to be specified in the setter or the getter, not both.
*/
PropertyCategory category() default PropertyCategory.UNSET;
/**
* If false, this property should not be accessible through Codeblocks.
* This was added to support the Row and Column properties, so they could
* be indirectly set in the Designer but not accessed in Codeblocks.
*/
boolean userVisible() default true;
}
category可以赋的值如下:
public enum PropertyCategory {
// TODO(user): i18n category names
BEHAVIOR("Behavior"),
APPEARANCE("Appearance"),
DEPRECATED("Deprecated"),
UNSET("Unspecified");
private String name;
PropertyCategory(String categoryName) {
name = categoryName;
}
public String getName() {
return name;
}
}
@DesignerProperty定义属性的类型和默认值,源码如下:
/**
*Annotation to mark properties to be visible in the ODE visual designer.
*
*<p>Only the setter method of the property must be marked with this
*annotation.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DesignerProperty {
/**
* Determines the property editor used in the designer.
*
* @return property type
*/
String editorType() default PropertyTypeConstants.PROPERTY_TYPE_TEXT;
/**
* Default value of property.
*
* @return default property value
*/
String defaultValue() default "";
}
editorType可以赋的值定义在PropertyTypeConstants.java里,在这里赋值为:
editorType =PropertyTypeConstants.PROPERTY_TYPE_NON_NEGATIVE_INTEGER
表示是非负整型。
/** 增加核查字符串长度的函数
* Check the length of test is ok or not.
*/
@SimpleFunction(
description = "Check the length of test is ok or not.")
public boolean CheckTextLength(){
if (textLength != 0){
if(Text().length() == textLength){
returntrue;
}else{
returnfalse;
}
}else{
//如果属性的值为0,则表示长度没有限制
return true;
}
}
@SimpleFunction是用于定义函数的注解,源码如下:
/**
*Annotation to mark Simple functions.
*
*<p>Note that the Simple compiler will only recognize Java methods marked
*with this annotation. All other methods will be ignored.
*
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SimpleFunction {
/**
* If non-empty, description to use in user-level documentation in placeof
* Javadoc, which is meant for developers.
*/
String description() default "";
/**
* If false, this property should not be accessible through Codeblocks.
* This was added (1) by analogy to {@link SimpleProperty#userVisible()}
* and (2) to temporarily hide functions for opening additional screens
* in {@link com.google.appinventor.components.runtime.Form}.
*/
boolean userVisible() default true;
}
定义好属性和函数后,还需要在OdeMessages.java中添加属性和函数的声明:
@DefaultMessage("TextLength")
@Description("")
String TextLengthProperties();
@DefaultMessage("CheckTextLength")
@Description("")
String CheckTextLengthMethods();
到目前为止,已经成功添加了属性和相关函数,可以正常使用了,但属性名称和函数名称在中文环境下也显示为英文,需要在OdeMessages_zh_CN.properties中添加中文字符串:
TextLengthProperties = 文本长度
CheckTextLengthMethods = 核查文本长度
最终的实现效果如下:
可以看到在组件属性中,多了一个设置文本长度的属性,在此设置值为6。
因为是添加在密码输入框和文本输入框组件的共同基类里的,所以密码输入框和文本输入框都继承并拥有了添加的属性和函数。
在密码输入框和文本输入框的工作面板中,都可以看到多了个核查文本长度的函数:
在工作面板中,也都可以设置文本长度:
使用示例如下:
相关文章推荐
- AI开发实战5-文本输入框(TextBox)的定制2
- AI开发实战3-定制自己的Screen
- AI开发实战10-文件管理器(File)的定制
- AI开发实战7-列表显示框(Listview)的定制
- AI开发实战6-图像组件(Image)的定制
- AI开发实战2-定制自己的AI伴侣
- AI开发实战8-Web浏览框(WebView)的定制1
- AI开发实战9-Web浏览框(WebView)的定制2
- 3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
- Unity实战 RTS3D即时战略游戏开发(十二) 战斗AI的控制
- 3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
- [转]Eclipse RCP应用系统开发方法与实战2-- 定制应用程序窗口属性
- 3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
- 3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
- 【UWP通用应用开发】编辑文本、绘制图形、3D透视效果及绘制时钟实战
- 【Android】安卓开发实战之自定义EditText输入框形状颜色
- Dota2 AI 开发 (二)定制AI阵容 配置英雄出装
- 3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
- 实现textbox对输入内容的自动检索功能,效果相当于百度首页的文本输入框
- [置顶]3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】