java 向 TextField 添加提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7010239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding hint to TextField
提问by AndroidHustle
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.
我想添加一个TextField
带有集成提示文本、用户提示、占位符的占位符,直到用户输入文本。当 TextField 获得焦点时提示文本消失,如果 TextField 失去焦点且未输入文本,则重新出现。
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.
我最初认为这将是 Vaadin TextFields 的通用功能,但似乎并非如此。现在我正在寻找一种方法来实现我自己的 TextField 扩展以添加此功能。但我被困住了。
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
}
}
回答by AndroidHustle
Built-in feature
内置功能
After some research I found that this is an integrated feature in all of the input controls (TextField, TextArea, DateField, ComboBox).
经过一番研究,我发现这是所有输入控件(TextField、TextArea、DateField、ComboBox)中的一个集成功能。
Vaadin Flow (Vaadin 10)
Vaadin 流 (Vaadin 10)
The feature is a property called Placeholder.
该功能是一个名为Placeholder的属性。
You can optionally pass the placeholder text to the constructor of TextField
, along with optional initial value.
您可以选择将占位符文本以及可选的初始值传递给 的构造函数TextField
。
new TextField( "label goes here" , "hint goes here" )
Or call the setter and getter: TextField::setPlaceholder
and TextField.getPlaceholder
.
或者调用 setter 和 getter: TextField::setPlaceholder
and TextField.getPlaceholder
。
myTextField.setPlaceholder( "Hint goes here" ) ;
Vaadin 8
瓦丁 8
The feature is a property called Placeholder.
该功能是一个名为Placeholder的属性。
Call the getter/setter methods: TextField::getPlaceholder
and TextField.setPlaceholder
.
调用 getter/setter 方法:TextField::getPlaceholder
和TextField.setPlaceholder
。
myTextField.setPlaceholder( "Hint goes here" ) ;
Vaadin 7
瓦丁 7
The feature is a property called InputPrompt.
该功能是一个名为InputPrompt的属性。
Call the getter/setter methods: TextField::setInputPrompt
and TextField::getInputPrompt
.
调用 getter/setter 方法:TextField::setInputPrompt
和TextField::getInputPrompt
。
myTextField.setInputPrompt("Hint goes here");