英文:
How to conditionally show/hide toolbar buttons in Fyne?
问题
我正在尝试让用户录制音频。当他们没有录制时,我想要一个"录制"按钮,当他们正在录制时,将其更改为"停止录制"按钮。此外,我期望这样做可以起作用,但是两个按钮始终都显示在工具栏上:
var startRecording, stopRecordingFunc func()
recordButton := widget.NewToolbarAction(theme.MediaRecordIcon(), startRecording)
stopButton := widget.NewToolbarAction(theme.MediaStopIcon(), stopRecordingFunc)
startRecording = func() {
if !recording {
recording = true
stopRecording = make(chan struct{})
recordWg.Add(1)
go func() {
recordAudio("output.wav", stopRecording, &recordWg)
recording = false
recordButton.ToolbarObject().Show()
stopButton.ToolbarObject().Hide()
}()
recordButton.ToolbarObject().Hide()
stopButton.ToolbarObject().Show()
}
}
stopRecordingFunc = func() {
if recording {
close(stopRecording)
recordWg.Wait()
recording = false
recordButton.ToolbarObject().Show()
stopButton.ToolbarObject().Hide()
}
}
recordButton.OnActivated = startRecording
stopButton.OnActivated = stopRecordingFunc
toolbar := widget.NewToolbar(recordButton, stopButton)
stopButton.ToolbarObject().Hide()
我尝试添加了Refresh()
的调用,但没有任何效果。
如何根据条件显示/隐藏工具栏按钮?
英文:
I'm trying to let users record audio. I want a 'record' button when they aren't recording, and when they are recording to change it to a 'stop recording' button. Furthermore, I expected this to work, but both buttons always show on the toolbar:
var startRecording, stopRecordingFunc func()
recordButton := widget.NewToolbarAction(theme.MediaRecordIcon(), startRecording)
stopButton := widget.NewToolbarAction(theme.MediaStopIcon(), stopRecordingFunc)
startRecording = func() {
if !recording {
recording = true
stopRecording = make(chan struct{})
recordWg.Add(1)
go func() {
recordAudio("output.wav", stopRecording, &recordWg)
recording = false
recordButton.ToolbarObject().Show()
stopButton.ToolbarObject().Hide()
}()
recordButton.ToolbarObject().Hide()
stopButton.ToolbarObject().Show()
}
}
stopRecordingFunc = func() {
if recording {
close(stopRecording)
recordWg.Wait()
recording = false
recordButton.ToolbarObject().Show()
stopButton.ToolbarObject().Hide()
}
}
recordButton.OnActivated = startRecording
stopButton.OnActivated = stopRecordingFunc
toolbar := widget.NewToolbar(recordButton, stopButton)
stopButton.ToolbarObject().Hide()
I've tried adding calls to Refresh()
but it makes no difference.
How can I conditionally show/hide toolbar buttons?
答案1
得分: 0
当你调用.ToolbarObject()
时,它会创建一个新的实例,该实例并不在工具栏中。
要操作工具栏的内容,你需要使用工具栏的API,或者直接使用ToolbarAction
上暴露的API。
从你的代码来看,最好使用SetIcon
来改变外观。
英文:
When you call .ToolbarObject()
it is creating a new instance - which is not in the toolbar at all.
To manipulate the contents of a toolbar you need to use the toolbar APIs, or those exposed directly on the ToolbarAction
.
From your code it looks like what you would be best using is SetIcon
to change the appearance.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论