Android:重命名 gui 元素与新布局

发布于 2024-11-07 15:19:28 字数 591 浏览 0 评论 0原文

我是 android 新手,正在处理以下问题。我需要创建一个按钮,将另外两个具有相似功能的按钮分组(某种子菜单)。假设我们有一个名为“搜索”的按钮,通过单击它,搜索按钮应该消失,并且屏幕上应该看到两个按钮(例如“google”和“bing”)。

因此,我的想法是以编程方式在同一布局中管理所有三个按钮,而不是创建一个新的(临时)布局只是为了显示和处理这两个按钮。就像一个状态机。更准确地说,它是这样的:

我们按下“搜索”按钮。 onClick(View) 确定我们是否在主屏幕上 (mode=mainmenu),将搜索按钮重命名为“google”并创建名为“bing”的第二个按钮,或者 (mode=submenu) 然后调用该按钮的函数搜索按钮已重命名为“google”...

通过按后退按钮 onBackPressed() 检查我们是否在子菜单中(mode=submenu),然后隐藏“bing”按钮并将“google”重命名为“搜索” ,否则我们在主菜单(mode=mainmenu)中,完成活动。

这有道理吗?此外,这将产生大量代码,其中按钮和(子)菜单的数量不断增加,并且进一步更改的可维护性更差。

谢谢!

PS 抱歉,如果我的英语很糟糕(不是我的母语);-)

I'm new into android and i'm dealing with the following problem. I need to create a button which groups another two buttons of similar features (sort of submenu). So let's say we have a button called "search", by clicking on it the search button should disappear and the two buttons (e.g. "google" and "bing") should be seen on the screen.

So, my idea was to manage all three buttons programmatically in the same layout instead of creating a new (temp)layout just to show and handle the two buttons. Like a state machine. More precisely it would be like this:

We press on the "search" button.
onClick(View) determines wheter we are on the main screen (mode=mainmenu), renames the search button to "google" and creates the second button named "bing", or (mode=submenu) then call the function of the button due to the search button is already renamed to "google"...

By pressing the back button onBackPressed() checks if we are in the submenu (mode=submenu) then hide the "bing" button and rename "google" to "search", otherwise we are in the main menu (mode=mainmenu), finish the activity.

Does this make sense at all? Besides this will produce lots of code with rising count of buttons and (sub)menus and worse maintainability for further changes.

Thanks!

P.S. Sorry if my english sucks (not my native language) ;-)

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

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

发布评论

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

评论(3

尤怨 2024-11-14 15:19:28

这需要太多的代码和太多的调试,所以我认为,更好的方法就是创建一个新的布局。

That's too much code and too much debugging will be needed, so I think, a better way is just creating a new layout.

蓝戈者 2024-11-14 15:19:28

好吧,如果您遇到性能问题或预计可能会出现性能问题(由于 GUI 的复杂性),那么这可能是有意义的。但如果不是,代码简单性比性能的轻微提高更重要。

Well if you have performance issues or expecting performance issues can arise (due to complexity of you GUI) then may be this make sense. But if not, code simplicity is more important than slight increase in performance.

醉酒的小男人 2024-11-14 15:19:28

您可以创建一个在按下“搜索”按钮之前保持不可见(确切地说是消失)的按钮,并重命名原始搜索按钮。它可以工作,并且如果您在 xml 中设置布局,那么实现起来会更容易。它还为您提供预览,显示其外观。以编程方式显示小部件将具有难以维护的代码。

然而,改变按钮文字的设计并不是一个好的设计。首先,当按下“搜索”时,更改其文本非常令人困惑。我个人建议你弹出一个选择对话框。或者只是在第一个位置显示两个搜索按钮,除非您确实没有地方放置两个按钮。

You can create an button that remain invisible(gone, to be exact) until the "search" button is pressed, and rename the original search button. It works, and it will be easier to implement if you set up layout in xml. It also give you a preview that shows what it looks like. To show widget programmatically will have code that hard to maintain.

However, the design that change button text is not a good design. First, when the "search" is pressed, changing its text is very confusing. I personally suggest you to pop-out a selection dialog. Or just show both search button at first place, unless you really do not have place for two button.

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