简体   繁体   English

当焦点对准文本框时,文本框应该折叠时,如何展开?

[英]How can i expand a textfield when it's in focus and when it's out of focus it should collapse?

I want to use a text field which will expand when the user enters some data and when the user is done editing and move out of text field than it should collapse. 我想使用一个文本字段,当用户输入一些数据时,当用户完成编辑并移出文本字段时,该文本字段将展开,而该文本字段应该折叠。 After that when ever user focus on the text field it should expand like a tooltip. 之后,当用户将注意力集中在文本字段上时,它应该像工具提示一样展开。 Any pointer regarding this will help me. 关于此的任何指示将对我有帮助。

Just add a FocusListener to your text field: 只需在您的文本字段中添加一个FocusListener即可:

text.addFocusListener(new FocusListener() {
    @Override
    public void focusGained(FocusEvent e) {
        text.setSize(); // put in the size you want
    }

    @Override
    public void focusLost(FocusEvent e) {
        text.setSize(); // put in the size you want
    }
});

Note that for setSize to work properly, parent of text can't have a layout. 请注意,为使setSize正常工作, text父级不能具有布局。

This code should give you an idea of how to do it: 这段代码应该使您了解如何执行此操作:

public static void main(String[] args)
{
    Display display = Display.getDefault();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    final Text text = new Text(shell, SWT.BORDER | SWT.MULTI);
    text.setText("edasdasdas\n\nasdasda\n\nasdasd");

    final GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
    data.heightHint = 100;

    text.setLayoutData(data);

    text.addListener(SWT.FocusIn, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            data.heightHint = 100;
            shell.layout(true);
        }
    });

    text.addListener(SWT.FocusOut, new Listener()
    {
        @Override
        public void handleEvent(Event arg0)
        {
            data.heightHint = 50;
            shell.layout(true);
        }
    });

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Button");

    shell.pack();
    shell.open();
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

It basically changes the heightHint of the GridData when focus is lost/gained. 当焦点丢失/获得时,它基本上会更改GridDataheightHint Afterwards you need to re-layout the parent. 之后,您需要重新布置父级。

You will have to make some adjustments for the "focused height" and "unfocused height" values yourself. 您必须自己对“聚焦高度”和“非聚焦高度”值进行一些调整。

Here are some screenshots: 以下是一些屏幕截图:

With focus: 重点关注:

在此处输入图片说明

Without focus: 没有重点:

在此处输入图片说明

Just press the Button to lose focus. 只需按一下Button即可失去焦点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Eclipse中失去焦点时,如何停止树视图展开/折叠图标褪色? - How can I stop the tree view expand/collapse icons from fading when losing focus in Eclipse? 如何聚焦Textfield? - How can I focus a Textfield? parentView折叠时如何清除childViewHolder焦点? - How to clear childViewHolder focus when parentView collapse? 焦点变化时如何替换经理的字段 - How to alternate a manager's fields when focus changes 单击活动的其他位置时如何使 SearchView 失去焦点并崩溃 - How to make SearchView lose focus and collapse when clicking elsewhere on activity 当TextField失去焦点时如何触发ChangeListener? JavaFX的 - How to trigger the ChangeListener when TextField loses focus ? JAVAFX 当我在Javafx中单击窗口中的其他位置时如何丢失文本字段焦点 - How to loss textfield focus when I click other place in window in Javafx 我想在 Jtextfield 上发生焦点事件时显示一个弹出菜单,生成弹出窗口但 t 不在该文本字段上它应该在的位置 - i want to show a popup menu when there is a focus event occur on a Jtextfield,popup is generated but t is not over that textfield where it should be java:表单加载时将焦点设置为文本字段 - java:set the focus to textfield when form load JavaFX - 当文本字段具有焦点时加速器不起作用 - JavaFX - Accelerator not working when textfield has focus
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM