前两天在调用TextArea的getSelectedText()时总是报索引值越界的错误,让我很是纳闷...
折腾了半天,搬开GXT源代码看。由于TextArea的getSelectedText()方法是从TextField方法继承而来.. so.. 在TextField.java中翻getSelectedText()的实现。
TextField中的getSelectedText()方法实现如下。
/**
* Returns the selected text.
*
* @return the selected text
*/
public String getSelectedText() {
int start = getCursorPos(), length = getSelectionLength();
return getRawValue().substring(start, start + length);
}
从此得知跟该方法有关的还有两个方法: getCursorPos()和getSelectionLength() 。。。
那么错误肯定处在这两个方法上, 于是写了个Demo来分别测试这两个方法, 最后发现是getCursorPos()无法在IE下取得正确的数值。
百般无奈之下,试了试GWT中的TextArea组件。。 惊奇的发现没有GXT中的错误。 取值很正确...
最后想了一个解决方案, 重新创建一个TextArea组件,继承与GWT的TextArea.. 然后使用Ext中的样式即可..
随后对Ext JS的TextArea组件的UI进行分析... 得出如下 css 样式设置:
1. 普通状态: 采用 x-form-field 样式
2. 获得焦点: 添加 x-form-focus 样式
3. 失去交单: 移除 x-form-focus 样式即可
具体代码如下:
import com.google.gwt.user.client.ui.FocusListener;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
public class TransferTextArea extends TextArea {
public TransferTextArea() {
this.setStyleName("x-form-field");
final TransferTextArea instance = this;
this.addFocusListener(new FocusListener() {
public void onFocus(Widget sender) {
instance.addStyleName("x-form-focus");
}
public void onLostFocus(Widget sender) {
instance.removeStyleName("x-form-focus");
}
});
}
}