简体   繁体   English

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

[英]Android: Renaming gui elements vs. new layout

I'm new into android and i'm dealing with the following problem. 我是android新手,正在处理以下问题。 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 (eg "google" and "bing") should be seen on the screen. 假设我们有一个名为“搜索”的按钮,通过单击它,搜索按钮应该消失,并且应该在屏幕上看到两个按钮(例如“ google”和“ bing”)。

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"... onClick(View)确定我们在主屏幕上后(mode = mainmenu),将搜索按钮重命名为“ google”,并创建第二个名为“ bing”的按钮,或者(mode = submenu)然后调用该按钮的功能搜索按钮已重命名为“ 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. 通过按下后退按钮onBackPressed()来检查我们是否在子菜单(mode = submenu)中,然后隐藏“ bing”按钮,并将“ google”重命名为“ search”,否则,我们就在主菜单中(mode = mainmenu),完成活动。

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! 谢谢!

PS Sorry if my english sucks (not my native language) ;-) PS对不起,如果我的英语不好(不是我的母语);-)

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. 它可以工作,并且如果您在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. 或仅将两个搜索按钮都放在第一位,除非您确实没有两个按钮的位置。

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

Well if you have performance issues or expecting performance issues can arise (due to complexity of you GUI) then may be this make sense. 好吧,如果您遇到性能问题或预期会出现性能问题(由于GUI的复杂性),那么这可能是有道理的。 But if not, code simplicity is more important than slight increase in performance. 但是,如果不是这样,那么代码的简单性比性能的轻微提高更为重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM