向 TextField 添加提示
我想添加一个带有集成提示文本、用户提示和占位符的 TextField
,直到用户输入文本。当 TextField 获得焦点时,提示文本会消失;如果 TextField 失去焦点且未输入任何文本,则提示文本会重新出现。
我最初认为这将是 Vaadin TextFields 的通用功能,但事实似乎并非如此。现在我正在寻找一种方法来实现我自己的 TextField 扩展以添加此功能。但我被困住了。
我的问题是,这里是否有人曾经这样做过,或者本能地知道应该如何做?
这就是我已经走了多远:
package com.smarttrust.m2m.gui.admin;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.terminal.gwt.client.ui.VCalendarPanel.FocusChangeListener;
import com.vaadin.ui.TextField;
public class M2MHintTextField extends TextField implements FocusListener {
private final String hint;
FocusListener listener = new FocusListener() {
@Override
public void focus(FocusEvent event) {
// TODO Auto-generated method stub
}
};
public M2MHintTextField(final String hint) {
super(hint);
this.hint = hint;
super.addListener(this.listener);
}
@Override
public void focus(FocusEvent event) {
// TODO Auto-generated method stub
}
}
I want to add a TextField
with an integrated hint text, a user prompt, a placeholder until the user enters text. The prompt text disappears when the TextField is focused and re-appears if the TextField loses focus and no text is entered.
I initially thought that this would be a generic feature for Vaadin TextFields but that doesn't seem to be the case. Now I'm looking for a way to implement my own extension of the TextField to add this feature. But I'm stuck.
My question is if anyone here has done this before or instinctively know how it should be done?
This is how far I've come:
package com.smarttrust.m2m.gui.admin;
import com.vaadin.event.FieldEvents.FocusEvent;
import com.vaadin.event.FieldEvents.FocusListener;
import com.vaadin.terminal.gwt.client.ui.VCalendarPanel.FocusChangeListener;
import com.vaadin.ui.TextField;
public class M2MHintTextField extends TextField implements FocusListener {
private final String hint;
FocusListener listener = new FocusListener() {
@Override
public void focus(FocusEvent event) {
// TODO Auto-generated method stub
}
};
public M2MHintTextField(final String hint) {
super(hint);
this.hint = hint;
super.addListener(this.listener);
}
@Override
public void focus(FocusEvent event) {
// TODO Auto-generated method stub
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
内置功能
经过一番研究,我发现这是所有输入控件(TextField、TextArea、DateField、ComboBox)中的集成功能。
Vaadin Flow (Vaadin 10)
该功能是一个名为 Placeholder 的属性。
您可以选择将占位符文本传递到
TextField
的构造函数,以及可选的初始值。或者调用 setter 和 getter:
TextField::setPlaceholder
和TextField.getPlaceholder
。Vaadin 8
该功能是一个名为 Placeholder 的属性。
调用 getter/setter 方法:
TextField::getPlaceholder
和TextField.setPlaceholder
。Vaadin 7
该功能是一个名为 InputPrompt 的属性。
调用 getter/setter 方法:
TextField::setInputPrompt
和TextField ::getInputPrompt
。Built-in feature
After some research I found that this is an integrated feature in all of the input controls (TextField, TextArea, DateField, ComboBox).
Vaadin Flow (Vaadin 10)
The feature is a property called Placeholder.
You can optionally pass the placeholder text to the constructor of
TextField
, along with optional initial value.Or call the setter and getter:
TextField::setPlaceholder
andTextField.getPlaceholder
.Vaadin 8
The feature is a property called Placeholder.
Call the getter/setter methods:
TextField::getPlaceholder
andTextField.setPlaceholder
.Vaadin 7
The feature is a property called InputPrompt.
Call the getter/setter methods:
TextField::setInputPrompt
andTextField::getInputPrompt
.