如何在Fyne中呼叫功能在失落的焦点上
我刚刚开始学习来自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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要触发该函数,您可以扩展
条目
和Overrideonfocuslost
(因为输入已经处理此)。但是,对于验证,最好使用验证器API,因为它可以自动为您的用户提供视觉反馈。我不确定您的示例是很多代码的陈述 - 每行似乎都在传达含义。
To trigger the function you could extend
Entry
and overrideOnFocusLost
(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.