4.2 增加私有的属性
如果想增加只属于文本输入框的属性,如增加一个属性,用户可设置文本输入框只是用于输入电子邮箱,则需要修改TextBox的代码:
//属性变量
private boolean acceptsEmailAddressOnly;
/**获取属性值的函数
* EmailAddress property getter method.
*
* @return {@code true} indicates that thetextbox accepts emailaddress only, {@code false} indicates
* that it accepts any text
*/
@SimpleProperty(
category= PropertyCategory.BEHAVIOR,
description= "If true, then this text box accepts only emailaddress as keyboardinput. ")
publicboolean EmailAddressOnly() {
return acceptsEmailAddressOnly;
}
/**设置属性值的函数
* EmailAddressOnly property setter method.
*
* @param acceptsEmailAddressOnly {@code true}restricts input to emailaddress,
* {@code false} allows any text
*/
@DesignerProperty(editorType= PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue ="False")
@SimpleProperty(
description= "If true, then this text box accepts only emailaddress as keyboardinput. ")
public voidEmailAddressOnly(boolean acceptsEmailAddressOnly) {
if (acceptsEmailAddressOnly) {
acceptsNumbersOnly= false;
view.setInputType(
InputType.TYPE_CLASS_TEXT|
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
} else {
view.setInputType(InputType.TYPE_CLASS_TEXT| InputType.TYPE_TEXT_FLAG_MULTI_LINE);
}
this.acceptsEmailAddressOnly = acceptsEmailAddressOnly;
}
定义好属性后,还需要在OdeMessages.java中添加属性的声明:
@DefaultMessage("EmailAddressOnly")
@Description("")
String EmailAddressOnlyProperties();
在OdeMessages_zh_CN.properties中添加中文字符串:
EmailAddressOnlyProperties = 仅限电子邮箱
最终的实现效果如下: