如何在Fyne中呼叫功能在失落的焦点上

发布于 2025-02-13 23:50:00 字数 942 浏览 8 评论 0原文

我刚刚开始学习来自JS/TS。 我想在失去专注方面验证Fyne的条目。

我这样做是这样的:

type dateEntry struct {
    widget.Entry
}

func NewDateEntry() *dateEntry {
    entry := &dateEntry{}
    entry.ExtendBaseWidget(entry)
    return entry
}

func (e *dateEntry) FocusLost() {
    println("Focus lost")
    e.Validate()
}

func main() {
    dateInput := NewDateEntry()
    dateInput.SetPlaceHolder("DD/MM/YYYY")
    dateInput.Validator = func(s string) (err error) {
        reDate := regexp.MustCompile("(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)")
        if s == "" {
            return fmt.Errorf("date required")
        } else if !reDate.MatchString(s) {
            return fmt.Errorf("date invalid")
        }
        return nil
    }
    w.SetContent(dateInput)
    w.ShowAndRun()
}

似乎有很多代码只是为了触发失去焦点的功能。有一种更简单的方法吗?

现在,在获得焦点时不再显示输入底部的蓝线,当处理焦点损失事件时,如何保持焦点时保持样式?

I just started learning Go coming from JS/TS.
I want to validate an entry in fyne on loss of focus.

I got it to work like this :

type dateEntry struct {
    widget.Entry
}

func NewDateEntry() *dateEntry {
    entry := &dateEntry{}
    entry.ExtendBaseWidget(entry)
    return entry
}

func (e *dateEntry) FocusLost() {
    println("Focus lost")
    e.Validate()
}

func main() {
    dateInput := NewDateEntry()
    dateInput.SetPlaceHolder("DD/MM/YYYY")
    dateInput.Validator = func(s string) (err error) {
        reDate := regexp.MustCompile("(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)")
        if s == "" {
            return fmt.Errorf("date required")
        } else if !reDate.MatchString(s) {
            return fmt.Errorf("date invalid")
        }
        return nil
    }
    w.SetContent(dateInput)
    w.ShowAndRun()
}

There seems to be a lot of code just to trigger a function on loss of focus. Is there a simpler way to do this?

Now the blue line at the bottom of the input is no longer displayed anymore when focus is gained, how can I keep the style when focus is gained while handling a focus loss event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

月竹挽风 2025-02-20 23:50:00

要触发该函数,您可以扩展条目和Override onfocuslost(因为输入已经处理此)。但是,对于验证,最好使用验证器API,因为它可以自动为您的用户提供视觉反馈。

我不确定您的示例是很多代码的陈述 - 每行似乎都在传达含义。

To trigger the function you could extend Entry and override OnFocusLost (Because Entry already handles this). However for validation it is better to use the Validator api as that provides visual feedback for your user automatically.

I’m not sure about the statement that your example is a lot of code - each line seems to be conveying meaning.

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