10

我创建了一个 CustomTextField,当我输入的文本超出使用 Horizo​​nalFieldManager 的 TextField 的宽度时,它会向左滚动但现在的问题是,如果我用鼠标右键单击并滚动它,它会继续不足的长度但并没有停止到最后一个字我输入这里有什么问题?它是一个错误

我只需要在到达最后一个单词时禁用 Horizo​​ntalScrolling 它应该能够在 word 中最后一个单词的开头和结尾之间滚动

查看代码

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.XYRect;
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.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;


public class CustomTextField extends VerticalFieldManager {
    private int textWidth=0;
    private int textHeight=0;
    private BasicEditField basicEditField;
    private HorizontalFieldManager hfm;
    //Border border;

    public CustomTextField(int width,int height) {
        super();
        textWidth=width;
        textHeight=height;
        //border=BorderFactory.createSimpleBorder(new XYEdges(1, 1, 1, 1)); 


        hfm=new HorizontalFieldManager(Manager.HORIZONTAL_SCROLL){
            protected void sublayout(int maxWidth, int maxHeight) {
                super.sublayout(maxWidth, maxHeight);
                setExtent(textWidth, textHeight);
            }
        };  
        basicEditField=new BasicEditField("","",200,BasicEditField.NO_NEWLINE);
        //basicEditField.setBorder(border);

        hfm.add(basicEditField);
        add(hfm);
    }


    protected void sublayout(int maxWidth, int maxHeight) {
        super.sublayout(textWidth, textHeight);
        setExtent(textWidth, textHeight);

    }


    protected void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.setColor(Color.BLACK);
        graphics.drawRect(0,0, textWidth, textHeight);
    }

}

我已将其初始化为

 CustomTextField textField=new CustomTextField(200, 20);
            add(textField);

我觉得 Horizo​​ntalFieldManager 需要 Scroll(is Scrolling Function) ......但还没有想出解决方案请帮忙

4

1 回答 1

4

因此,在 BlackBerry 字段中,范围是字段的实际可视大小。但是,虚拟范围是它可以使用的逻辑大小,其中一些可能不可见。对于Managers您想要滚动的内容,您通常会将虚拟范围设置为大于范围。

我使用这个概念来动态更改您的虚拟范围HorizontalFieldManager,基于当前需要多少空间才能勉强容纳BasicEditField. 为此,我必须让HorizontalFieldManager监听器BasicEditField通过实现FieldChangeListener. 然后,当每个字符输入到编辑字段中时,水平字段管理器将重新计算当前字段中的文本量需要多少宽度。然后它将虚拟宽度重新设置为该宽度。

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

所以,我不认为黑莓做错了什么......操作系统中没有错误。以前,只是没有设置虚拟范围。

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

其他想法:

1) 由于您尝试使用自定义paint()实现绘制边框,因此存在绘图伪影。但是,那个 bug 本来就在那里,我把这个问题解释为关于滚动问题。看起来您正在尝试使用Border对象,这可能是实现滚动字段边框的更好方法。

2)使用我的新解决方案,实际CustomTextField课程中没有太多内容。它只是Manager. CustomHorizontalFieldManager如果你愿意,你可能会摆脱那个外层。但是,我知道有时当您发布代码时,您会删除对您遇到的问题并不重要的细节。所以,也许需要一个VerticalFieldManager包含 a 的HorizontalFieldManager包含 a BasicEditField。我会把它留给你......不过,它只是可选的cleanup

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;
      /** the maximum virtual width of the edit field, based on the max num of chars */
      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);
         }
      }
   }

}
于 2012-05-20T09:42:37.947 回答