这是我自动调整大小的TextArea的代码(我使用Errai,所以TextArea注入了btw):
@Inject
@DataField
TextArea content;
@AfterInitialization
public void afterInit(){
content.getElement().setAttribute("wrap","off");
content.addKeyUpHandler(new KeyUpHandler() {
@Override
public void onKeyUp(KeyUpEvent event) {
$(content)
.css("overflow-y", "hidden")
.css("overflow-x", "auto")
.css(CSS.HEIGHT, "300px")
.css(CSS.HEIGHT, DOM.getElementPropertyInt(content.getElement(),"scrollHeight") + "px");
}
});
content.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
$(content)
.css("overflow-y", "hidden")
.css("overflow-x", "auto")
.css(CSS.HEIGHT, "300px")
.css(CSS.HEIGHT, DOM.getElementPropertyInt(content.getElement(),"scrollHeight") + "px");
}
});
content.addMouseUpHandler(new MouseUpHandler() {
@Override
public void onMouseUp(MouseUpEvent event) {
$(content)
.css("overflow-y", "hidden")
.css("overflow-x", "auto")
.css(CSS.HEIGHT, "300px")
.css(CSS.HEIGHT, DOM.getElementPropertyInt(content.getElement(),"scrollHeight") + "px");
}
});
}
这段代码很好用,我想注意几个问题: