AI开发实战4-文本输入框(TextBox)的定制1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xjbclz/article/details/77488060

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。

因为是添加在密码输入框和文本输入框组件的共同基类里的,所以密码输入框和文本输入框都继承并拥有了添加的属性和函数。

在密码输入框和文本输入框的工作面板中,都可以看到多了个核查文本长度的函数:

 

在工作面板中,也都可以设置文本长度:

 

 

使用示例如下:

猜你喜欢

转载自blog.csdn.net/xjbclz/article/details/77488060