栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

水平滚动和TextField错误

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

水平滚动和TextField错误

因此,在BlackBerry字段中, 范围 是字段的实际 视觉 尺寸。但是, 虚拟范围 是它可以使用的 逻辑
大小,其中一些可能不可见。为此

Managers
,您通常需要将虚拟范围设置为大于范围。

我使用这个概念来动态地改变您的虚拟范围

HorizontalFieldManager
,根据当前需要多少空间来勉强适应文本的大小
BasicEditField
。为此,我必须通过实现
HorizontalFieldManager
来监听对的更改。然后,在将每个字符输入到编辑字段中时,水平字段管理器将重新计算该字段中现在需要多少宽度的文本。然后,将虚拟宽度重新设置为该宽度。
BasicEditField``FieldChangeListener

这导致水平字段管理器仅允许滚动到输入文本的末尾,而不是向右滚动,这就是代码最初的工作方式。

因此,我认为BlackBerry没有做错任何事情……在操作系统中没有错误。以前,还没有设置虚拟范围。

我将您的Horizo​​ntalFieldManager拆分为一个新的私有类,因为当逻辑超过大约5行代码时,我不喜欢使用匿名类。因此,下面的解决方案看起来有些不同。

其他想法:

1)由于您尝试使用自定义

paint()
实现绘制边框,因此出现了一些绘图工件。但是,该错误最初存在于此,我将这个问题解释为与滚动问题有关。似乎您正在尝试使用
Border
对象,这可能是实现滚动字段边框的更好方法。

2)在我的新解决方案中,实际的

CustomTextField
类没有太多内容。它只是的容器(
Manager
CustomHorizontalFieldManager
。如果需要,您可能会摆脱该外层。但是,我知道有时候在发布代码时,您会删除一些细节,这些细节对于遇到麻烦的事情并不重要。因此,可能需要
VerticalFieldManager
包含一个
HorizontalFieldManager
包含一个
BasicEditField
的容器。我会留给您…不过这只是可选的
清理

3)我在5.0 Storm2模拟器上对此进行了测试。

import net.rim.device.api.ui.Color;import net.rim.device.api.ui.Field;import net.rim.device.api.ui.FieldChangeListener;import net.rim.device.api.ui.Graphics;import net.rim.device.api.ui.Manager;import net.rim.device.api.ui.component.BasicEditField;import net.rim.device.api.ui.container.HorizontalFieldManager;import net.rim.device.api.ui.container.VerticalFieldManager;import net.rim.device.api.util.Arrays;public class CustomTextField extends VerticalFieldManager {   private int textWidth = 0;   private int textHeight = 0;   private CustomHorizontalFieldManager hfm;   public CustomTextField(int width, int height) {      super();      textWidth = width;      textHeight = height;      hfm = new CustomHorizontalFieldManager();      add(hfm);   }   protected void sublayout(int maxWidth, int maxHeight) {      super.sublayout(textWidth, textHeight);      setExtent(textWidth, textHeight);   }   protected void paint(Graphics graphics) {      // TODO: change me!      super.paint(graphics);      graphics.setColor(Color.BLACK);      graphics.drawRect(0, 0, textWidth, textHeight);   }   private class CustomHorizontalFieldManager extends HorizontalFieldManager implements FieldChangeListener {      private BasicEditField basicEditField;            private int maxVirtualWidth;      public CustomHorizontalFieldManager() {         super(Manager.HORIZONTAL_SCROLL);         int maxNumChars = 200;         basicEditField = new BasicEditField("", "", maxNumChars, BasicEditField.NO_newline);         // determine how wide the field would need to be to hold 'maxNumChars', with the font         //   in use ... just pick a long string of all W's, since that's usually the widest char         char[] buffer = new char[maxNumChars];         Arrays.fill(buffer, 'W');         String spacer = new String(buffer);         maxVirtualWidth = basicEditField.getFont().getAdvance(spacer);         // we need to listen as the user types in this field, so we can dynamically alter its         //   virtual width         basicEditField.setChangeListener(this);         add(basicEditField);      }      protected void sublayout(int maxWidth, int maxHeight) {         super.sublayout(maxWidth, maxHeight);         // extent is the visible size, virtual extent can be wider if we want scrolling         setExtent(textWidth, textHeight);         setVirtualExtent(maxVirtualWidth, textHeight);      }      public void fieldChanged(Field f, int context) {         if (f == basicEditField) { // recalculate how much virtual width the edit field needs, based on the  //  current text content int newWidth = basicEditField.getFont().getAdvance(basicEditField.getText()); setVirtualExtent(newWidth, textHeight);         }      }   }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/508646.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号