黑莓和 Editfields
这是一个非常基本的事情,但我似乎无法在网上搜索找到答案。
基本上,我在 HorizontalFieldManager 上显示两个相邻的 EditField 时遇到问题。
我相信从我读到的内容来看,EditField 使用它所在容器的最大宽度。考虑到这一点,我认为将 editField 添加到单独的 VerticalFieldManager 中,然后将它们添加到我的 HorizontalFieldManager 中就可以了。下面是我这样做的更多代码。但不幸的是,用户名显示正常,但密码字段无法显示。关于我做错了什么有什么想法吗?
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.USE_ALL_WIDTH));
VerticalFieldManager vfmUser = new VerticalFieldManager();
VerticalFieldManager vfmPassw = new VerticalFieldManager(;
userName = new EditField("Username:", "", 5, EditField.FIELD_LEFT);
password = new PasswordEditField("Pass: ", "", 5, PasswordEditField.FIELD_RIGHT);
vfmUser.add(userName);
vfmPassw.add(password);
hfm.add(vfmUser);
hfm.add(vfmPassw);
add(hfm);
This is a very basic thing but I cant seem to find the answer searching then net.
Basically Im having issues in displaying two EditFields next to each other on a HorizontalFieldManager.
I believe from what Ive read the EditField uses the maximium width of the container it is in. With this in mind I thought adding the editFields to seperate VerticalFieldManagers and then adding these to my HorizontalFieldManager would work. Below is moreoless the code im doing this. But unfortuntely the userName show fine but the password field fails to dispplay. Any ideas on what im doing wrong?
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.USE_ALL_WIDTH));
VerticalFieldManager vfmUser = new VerticalFieldManager();
VerticalFieldManager vfmPassw = new VerticalFieldManager(;
userName = new EditField("Username:", "", 5, EditField.FIELD_LEFT);
password = new PasswordEditField("Pass: ", "", 5, PasswordEditField.FIELD_RIGHT);
vfmUser.add(userName);
vfmPassw.add(password);
hfm.add(vfmUser);
hfm.add(vfmPassw);
add(hfm);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是 EditField 仍会尝试使用所有宽度,这会导致 VerticalFieldManager 也使用可用的整个宽度,从而有效地推开密码字段。
据我所知,执行此类操作的唯一方法是创建类,将其宽度显式设置为屏幕的 50%。有两种方法可以执行此操作:创建 EditField 和 PasswordEditField 的子类,或者创建 HorizontalFieldManager 的子类。前一种选择在不同平台上会更容易、更可靠,但需要两个类而不是一个类。
为此,您需要创建 EditField 的子类并重写其
layout
方法,如下所示:对于 PasswordEditField 也类似。
根据您希望它的行为方式,您可能还必须重写其他方法。例如,这段代码(我很确定,没有具体测试过)会导致任何额外的字符在不适合所需区域的情况下被截断。如果这不是您想要的,您可能必须覆盖
paint
或根据当前文本更改布局。另外,您应该在倾斜风暴屏幕时对其进行测试,以确保尺寸更新正确 - 它应该在大多数情况下工作,但如果您在屏幕上有某些管理器配置,则有一个错误会导致它无法更新布局/尺寸。如果您决定采用管理器路线(同样,我并不特别推荐,因为您可能会遇到更多问题),您将需要扩展 Manager 或 HorizontalFieldManager (前者可能更容易) ,因此您不必处理 HFM 的一些怪癖),并重写其
sublayout
方法。在此方法中,您需要为管理器中的每个字段(在您的情况下,用户字段和密码字段)调用layoutChild
和setPositionChild
(按顺序) )。然后,最后,您需要调用setExtent
。我建议您参考有关此事的黑莓文档,但老实说,您最好搜索如何实施黑莓自定义管理器,或提出相关问题。话虽如此,您可能还想重新考虑为什么要这样做。我可以根据经验向您保证,有些手机的 UI 看起来不正确 - 我们曾经尝试过类似的方法,但最终放弃了它,因为从 UI 的角度来看,我们无法让它在所有设备上工作。有许多不同的分辨率 -有些非常窄,有些非常宽 - 如果您使用此用户界面,那么某些设备的登录将非常狭窄。恕我直言,最好与其他黑莓应用程序保持一致,并将用户名和密码放在不同的行上。
The problem is that the EditField will still try to use all of the width, which causes the VerticalFieldManager to also use the entire width available to it, effectively shoving off the password field.
The only way I know of to do something like this would be to create classes that explicitly set their width to 50% of the screen. There are two ways of doing this: either create subclasses of EditField and PasswordEditField, or create a subclass of HorizontalFieldManager. The former option will be easier and more reliable across the different platforms, but will require two classes instead of one.
To do this, you'll want to create a subclass of EditField and override its
layout
method to look something like:Similarly for the PasswordEditField.
Depending on how you want it to behave, you might have to override other methods as well. For instance, this code will (I'm pretty certain, haven't tested it specifically) cause any extra characters to be truncated if they dont fit in the desired area. If that's not what you want, you might have to override
paint
or change the layout based on the current text. Also, you should test it when tilting the storm screen to ensure the size updates correctly - it should work in most cases, but there is a bug that causes it to not update layouts/sizes if you have certain configurations of managers on the screen.If you decide to go the manager route (which, again, I don't especially recommend, as you'll probably run into many more issues), you'll want to extend either Manager or HorizontalFieldManager (might be easier to do the former, so you don't have to deal with some of the quirks of the HFM), and override its
sublayout
method. In this method, you'll want to calllayoutChild
andsetPositionChild
(in that order) for every field in the manager (in your case, the user field and the password field). Then, at the end, you'll want to callsetExtent
. I would refer you to the blackberry docs on this matter, but in all honesty you're better off searching for how to implement blackberry custom managers, or asking questions on SO.All that said, you might also want to reconsider why you want to do this. I can guarantee you from experience that there will be some phones where the UI does not look right - we tried a similar approach once, and eventually scrapped it because we couldn't get it to work from a UI standpoint on all devices. There are lots of different resolutions - some very narrow, some very wide - and if you go with this UI then some devices will have very cramped logins. IMHO, it would be preferable to be consistent with the other BlackBerry applications, and have username and password on different lines.
HorizontalFieldManager hfm = new HorizontalFieldManager();
试试这个代码。我会解决你的问题。
HorizontalFieldManager hfm = new HorizontalFieldManager();
try this code. i will be the solution of your problem.