3

我的 GWT 页面有一个 TextArea,我希望它具有焦点并在加载此页面时选择所有文本。我尝试下面的代码,但它根本不起作用。你能帮助我吗?谢谢

final TextArea myText = new TextArea();
myText.setCharacterWidth(50);
myText.setVisibleLines(20);
myText.setText("Just try something");
RootPanel.get("textContainer").add(myText);
myText.setVisible(true);
myText.setFocus(true);
myText.selectAll();
4

1 回答 1

4

的文档TextBox.selectAll()说:

This will only work when the widget is attached to the document and not hidden.

TextBox您调用.selectAll().

尝试使用Scheduler

final TextArea myText = new TextArea();
myText.setCharacterWidth(50);
myText.setVisibleLines(20);
myText.setText("Just try something");
RootPanel.get("textContainer").add(myText);
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
        @Override
        public void execute() {
            // your commands here
            myText.setVisible(true);
            myText.setFocus(true);
            myText.selectAll();
        }
});
于 2011-08-01T10:41:35.607 回答