向TextField添加提示 [英] Adding hint to TextField
问题描述
我想添加带有集成提示文本,用户提示和占位符的TextField
,直到用户输入文本为止.当TextField处于焦点状态时,提示文本消失,如果TextField失去焦点且未输入任何文本,则提示文本会重新出现.
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.
我最初认为这将是Vaadin TextFields的通用功能,但事实并非如此.现在,我正在寻找一种实现自己的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.
我的问题是,这里的人是否曾经做过此事或本能地知道应该怎么做?
My question is if anyone here has done this before or instinctively know how it should be done?
这是我走了多远:
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
}
}
推荐答案
内置功能
经过研究,我发现这是所有输入控件(TextField,TextArea,DateField,ComboBox)中的集成功能.
Built-in feature
After some research I found that this is an integrated feature in all of the input controls (TextField, TextArea, DateField, ComboBox).
该功能是一个名为 Placeholder 的属性.
The feature is a property called Placeholder.
You can optionally pass the placeholder text to the constructor of TextField
, along with optional initial value.
new TextField( "label goes here" , "hint goes here" )
或致电设置器和获取器: TextField.getPlaceholder
.
Or call the setter and getter: TextField::setPlaceholder
and TextField.getPlaceholder
.
myTextField.setPlaceholder( "Hint goes here" ) ;
Vaadin 8
该功能是一个名为 Placeholder 的属性.
调用getter/setter方法: TextField.setPlaceholder
.
Call the getter/setter methods: TextField::getPlaceholder
and TextField.setPlaceholder
.
myTextField.setPlaceholder( "Hint goes here" ) ;
Vaadin 7
该功能是名为 InputPrompt 的属性.
调用getter/setter方法: TextField::getInputPrompt
.
Call the getter/setter methods: TextField::setInputPrompt
and TextField::getInputPrompt
.
myTextField.setInputPrompt("Hint goes here");
这篇关于向TextField添加提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!