向 TextField 添加提示

发布于 2024-11-29 08:51:30 字数 1109 浏览 1 评论 0原文

我想添加一个带有集成提示文本、用户提示和占位符的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

马蹄踏│碎落叶 2024-12-06 08:51:31

内置功能

经过一番研究,我发现这是所有输入控件(TextField、TextArea、DateField、ComboBox)中的集成功能。

Vaadin Flow (Vaadin 10)

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

您可以选择将占位符文本传递到 TextField 的构造函数,以及可选的初始值。

new TextField( "label goes here" , "hint goes here" ) 

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

myTextField.setPlaceholder( "Hint goes here" ) ;

Vaadin 8

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

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

myTextField.setPlaceholder( "Hint goes here" ) ;

Vaadin 7

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

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

myTextField.setInputPrompt("Hint goes here"); 

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.

new TextField( "label goes here" , "hint goes here" ) 

Or call the setter and getter: TextField::setPlaceholder and TextField.getPlaceholder.

myTextField.setPlaceholder( "Hint goes here" ) ;

Vaadin 8

The feature is a property called Placeholder.

Call the getter/setter methods: TextField::getPlaceholder and TextField.setPlaceholder.

myTextField.setPlaceholder( "Hint goes here" ) ;

Vaadin 7

The feature is a property called InputPrompt.

Call the getter/setter methods: TextField::setInputPrompt and TextField::getInputPrompt.

myTextField.setInputPrompt("Hint goes here"); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文