Golang Fyne v2自定义输入小部件无法覆盖FocusGained()函数吗?

huangapple go评论86阅读模式
英文:

Golang Fyne v2 custom entry widget can not overrule the FocusGained() function?

问题

我目前正在使用Golang Fyne V2框架进行工作。我想在我的输入小部件被点击/选中时调用一个自定义函数。我的方法是创建一个自定义输入小部件。

问题是我的自定义小部件没有覆盖以下函数:

func (e *Entry) Tapped(ev *fyne.PointEvent)

或者

func (e *Entry) FocusGained()

我的自定义输入字段基本上是这样的。我在Gui容器中看到了渲染的输入字段,所以很好。但是当我点击它时,“EXTRA FUNCTION CALL”没有被打印出来。

package gui

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/driver/desktop"
	"fyne.io/fyne/v2/widget"
)

type InputField struct {
	widget.BaseWidget
	inputText      *widget.Entry
	customFunction func()
}

func NewInputField() *InputField {

	i := &InputField{
		BaseWidget: widget.BaseWidget{},
		inputText:  widget.NewEntry(),
	}

	i.inputText.Resize(i.MinSize())


	i.customFunction = func() {
		fmt.Println("EXTRA FUNCTION CALL")
	}

	i.ExtendBaseWidget(i)

	return i
}

func (i *InputField) MinSize() fyne.Size {
	return fyne.NewSize(600, 30)
}

func (i *InputField) Tapped(ev *fyne.PointEvent) {
	fmt.Println("I have been tapped")
	i.customFunction()
}

func (i *InputField) FocusGained() {
	fmt.Println("I have been focussed")
	i.customFunction()
}

func (i *InputField) FocusLost() {
	fmt.Println("Focus lost")
}

func (i *InputField) MouseDown(m *desktop.MouseEvent) {
	fmt.Println("MouseDown")
	i.customFunction()
}

func (t *InputField) CreateRenderer() fyne.WidgetRenderer {
	return NewBaseRenderer([]fyne.CanvasObject{t.inputText})
}

英文:

Im currently working in Golang Fyne V2 framework. I want to call a custom function when my entry widget has been clicked/selected. My approach was to make a custom entry widget.
https://developer.fyne.io/api/v2.0/widget/entry.html

Problem is that my custom widget is not overruling the following function:

func (e *Entry) Tapped(ev *fyne.PointEvent)

OR

func (e *Entry) FocusGained()

My custom input field looks basicly like this. I see the inputField rendered in a Gui container so thats great. But when I click on it the "EXTRA FUNCTION CALL" is not being printed.

package gui
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/widget"
)
type InputField struct {
widget.BaseWidget
inputText      *widget.Entry
customFunction func()
}
func NewInputField() *InputField {
i := &InputField{
BaseWidget: widget.BaseWidget{},
inputText:  widget.NewEntry(),
}
i.inputText.Resize(i.MinSize())
i.customFunction = func() {
fmt.Println("EXTRA FUNCTION CALL")
}
i.ExtendBaseWidget(i)
return i
}
func (i *InputField) MinSize() fyne.Size {
return fyne.NewSize(600, 30)
}
func (i *InputField) Tapped(ev *fyne.PointEvent) {
fmt.Println("I have been tapped")
i.customFunction()
}
func (i *InputField) FocusGained() {
fmt.Println("I have been focussed")
i.customFunction()
}
func (i *InputField) FocusLost() {
fmt.Println("Focus lost")
}
func (i *InputField) MouseDown(m *desktop.MouseEvent) {
fmt.Println("MouseDown")
i.customFunction()
}
func (t *InputField) CreateRenderer() fyne.WidgetRenderer {
return NewBaseRenderer([]fyne.CanvasObject{t.inputText})
}

答案1

得分: 1

不要扩展BaseWidgetEntry,直接扩展widget.Entry
通过这个改变,它应该按照你的期望工作。

英文:

Don’t extend BaseWidget and Entry, just extend widget.Entry directly.
With that change it should work as you expect.

huangapple
  • 本文由 发表于 2022年11月16日 18:14:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/74458702.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定