水平滚动和TextField错误 [英] Horizontal Scrolling and TextField Error

查看:126
本文介绍了水平滚动和TextField错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个CustomTextField
 这本身滚动向左当我键入文本这是比文本字段的宽度额外
 对于这一个Horizo​​nalFieldManager使用
 但现在的问题是,如果我右键我的鼠标点击和滚动它
 它接着长度不足
 但不会停下来的最后一个字i型
这里有什么问题?
它是一个错误

I have created a CustomTextField which scrolls itself towards left when i type text which are extra than the width of the TextField for which a HorizonalFieldManager is used But now the problem is if i Right click with my mouse and scroll it it goes on to inadequate length but does not stop to the last word i type What is the problem here ?? Is it a bug

我只需要,要当它到达最后字禁用Horizo​​ntalScrolling
它应该能够仅仅是个开始,并在字最后一个字的结尾之间滚动

I just need that to disable HorizontalScrolling when it reaches the last word It should be able to scroll just between the start and end of last word in word

检查出code

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的需要......但还没有走到尚未解决
请帮助

I feel the need of Scroll(is Scrolling Function) for HorizontalFieldManager ... but have not yet came up to solution yet Please help

推荐答案

所以,在黑莓手机领域,在范围是一个字段的实际的视觉的大小。但是,在虚拟程度逻辑的大小也可以使用,其中一些可能是不可见的。对于管理​​你想拥有卷轴,你通常会设置虚拟程度比大程度

So, in BlackBerry fields, the extent is the actual visual size of a field. But, the virtual extent is the logical size it can use, some of which may not be visible. For Managers that you want to have scroll, you would normally set the virtual extent to be larger than the extent.

我用这个概念来动态改变的虚拟程度的 Horizo​​ntalFieldManager 的基础上,目前正在多大的空间需要刚好契合了<$ C $文本C> BasicEditField 。要做到这一点,我不得不让 Horizo​​ntalFieldManager 监听更改 BasicEditField ,通过实施 FieldChangeListener 。然后,当每个字符键入到编辑字段中,水平场管理器将重新计算多少宽度是需要文字组成,现在该领域的量。然后将其重新设置虚拟宽度的宽度。

I used this concept to dynamically change the virtual extent of your HorizontalFieldManager, based on how much space was currently needed to just barely fit the text in the BasicEditField. To do this, I had to let the HorizontalFieldManager listen for changes to the BasicEditField, by implementing FieldChangeListener. Then, as each character is typed into the edit field, the horizontal field manager will recalculate how much width is needed for the amount of text that's now in the field. It then re-sets the virtual width to that width.

这会导致水平场经理只允许滚动到文本的末尾进入,没有办法的权利,这是code最初是如何工作。

This causes the horizontal field manager to only allow scrolling to the end of the text entered, not way to the right, which is how the code originally worked.

所以,我不认为黑莓是做错了什么......在OS没有错误。 previously,虚拟程度只是没有设置。

So, I don't think the BlackBerry was doing anything wrong ... no bug in the OS. Previously, the virtual extent just wasn't set.

我分裂你的Horizo​​ntalFieldManager到一个新的私有类,因为我不喜欢当逻辑超过约5 code线使用匿名类。所以,下面的解决方案看起来有点不同。

I split your HorizontalFieldManager into a new private class, because I don't like using anonymous classes when the logic exceeds about 5 lines of code. So, the solution below looks a little different.

其他的想法:

1)有绘画文物为你试图绘制一个边框与自定义的paint()执行的结果。但是,该错误原是那里,我间pretted这个问题是关于滚动问题。它看起来像你试图用边框的对象,这可能是更好的方式来实现边框滚动现场。

1) There are drawing artifacts as a result of your attempt to draw a border with a custom paint() implementation. But, that bug was originally there, and I interpretted this question to be about the scrolling problem. It looks like you were trying to use Border objects, which is probably the better way to achieve a border for a scrolling field.

2),与我的新的解决方案,实际的 CustomTextField 类没有在里面了。这只是一个容器(管理​​)的 CustomHorizo​​ntalFieldManager 。如果你想,你也许可以摆脱外层。但是,我知道,有时当你张贴code,你删除不给您遇到麻烦事情的重要细节。所以,也许有一个 VerticalFieldManager 包含 Horizo​​ntalFieldManager 包含一个 BasicEditField 是必要的。我把它留给你......它只会是可选的清除的,虽然。

2) With my new solution, the actual CustomTextField class doesn't have much in it. It's just a container (Manager) for the CustomHorizontalFieldManager. You could probably get rid of that outer layer if you wanted. But, I know that sometimes when you post code, you remove details that aren't important to the thing you're having trouble with. So, maybe having a VerticalFieldManager contain a HorizontalFieldManager which contains a BasicEditField is needed. I'll leave that to you ... it would only be optional cleanup, though.

3)我测试的5.0风暴2模拟器。

3) I tested this on a 5.0 Storm2 simulator.

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);
         }
      }
   }

}

这篇关于水平滚动和TextField错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆