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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:16:09  来源:igfitidea点击:

Adding hint to TextField

javavaadintextfieldprompthint

提问by AndroidHustle

I want to add a TextFieldwith 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::setPlaceholderand TextField.getPlaceholder.

或者调用 setter 和 getter: TextField::setPlaceholderand TextField.getPlaceholder

myTextField.setPlaceholder( "Hint goes here" ) ;

Vaadin 8

瓦丁 8

The feature is a property called Placeholder.

该功能是一个名为Placeholder的属性。

Call the getter/setter methods: TextField::getPlaceholderand TextField.setPlaceholder.

调用 getter/setter 方法:TextField::getPlaceholderTextField.setPlaceholder

myTextField.setPlaceholder( "Hint goes here" ) ;

Vaadin 7

瓦丁 7

The feature is a property called InputPrompt.

该功能是一个名为InputPrompt的属性。

Call the getter/setter methods: TextField::setInputPromptand TextField::getInputPrompt.

调用 getter/setter 方法:TextField::setInputPromptTextField::getInputPrompt

myTextField.setInputPrompt("Hint goes here");