2012-05-16 32 views
10

Tôi đã tạo một CustomTextField cuộn tự về phía trái khi tôi nhập văn bản vượt quá chiều rộng của TextField mà HorizonalFieldManager được sử dụng Nhưng bây giờ vấn đề là nếu tôi nhấp chuột phải chuột của tôi và cuộn nó nó đi vào chiều dài không đủ nhưng không dừng lại cho từ cuối cùng tôi gõ vấn đề ở đây là gì ?? Có một lỗiCuộn ngang và lỗi văn bản

Tôi chỉ cần điều đó để vô hiệu hóa HorizontalScrolling khi nó đạt đến chữ cuối cùng Nó sẽ có thể di chuyển chỉ giữa đầu và cuối của từ cuối cùng trong từ

Kiểm tra mã

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

} 

tôi đã khởi tạo nó như

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

tôi cảm thấy sự cần thiết của Scroll (được cuộn funct ion) cho HorizontalFieldManager ... nhưng chưa đưa ra để giải pháp được nêu Xin giúp

+0

"PHẢI Nhấp" bạn đang sử dụng trình mô phỏng? Bạn đã kiểm tra nó trong thiết bị chưa? – Aditya

+0

Có .. nó giống nhau trong Thiết bị – Yatin

+0

@HeartBeat: bạn có thể giải quyết vấn đề này không ?? – Yatin

Trả lời

4

Vì vậy, trong các lĩnh vực BlackBerry, mức độ là thực tế thị giác kích thước của một lĩnh vực. Tuy nhiên, mức độ ảo là kích thước hợp lý mà nó có thể sử dụng, một số có thể không hiển thị. Đối với Managers mà bạn muốn có cuộn, bạn thường sẽ đặt mức ảo là lớn hơn mức độ.

Tôi đã sử dụng khái niệm này để tự động thay đổi mức ảo của HorizontalFieldManager, dựa trên số lượng không gian hiện cần để vừa đủ nội dung trong BasicEditField. Để làm điều này, tôi phải để số HorizontalFieldManager lắng nghe các thay đổi đối với BasicEditField, bằng cách thực hiện FieldChangeListener. Sau đó, khi mỗi ký tự được nhập vào trường chỉnh sửa, trình quản lý trường ngang sẽ tính toán lại số lượng chiều rộng cần thiết cho số lượng văn bản hiện có trong trường. Sau đó nó đặt lại chiều rộng ảo thành chiều rộng đó.

Điều này làm cho trình quản lý trường ngang chỉ cho phép cuộn đến cuối văn bản được nhập, không phải theo cách bên phải, đó là cách mã ban đầu hoạt động.

Vì vậy, tôi không nghĩ rằng BlackBerry đã làm bất cứ điều gì sai ... không có lỗi trong hệ điều hành. Trước đây, mức ảo chỉ chưa được thiết lập.

Tôi chia HorizontalFieldManager của bạn thành một lớp riêng tư mới, bởi vì tôi không thích sử dụng các lớp ẩn danh khi logic vượt quá khoảng 5 dòng mã. Vì vậy, giải pháp bên dưới trông hơi khác một chút.

suy nghĩ khác:

1) Có bản vẽ hiện vật là kết quả của nỗ lực của bạn để vẽ một biên giới với một phong tục paint() thực hiện. Nhưng, lỗi đó ban đầu ở đó, và tôi giải thích câu hỏi này là về vấn đề di chuyển. Có vẻ như bạn đang cố gắng sử dụng các đối tượng Border, đây có lẽ là cách tốt hơn để đạt được đường viền cho trường cuộn.

2) Với giải pháp mới của tôi, lớp thực tế CustomTextField không có nhiều trong đó.Nó chỉ là một container (Manager) cho CustomHorizontalFieldManager. Bạn có thể có thể thoát khỏi lớp bên ngoài đó nếu bạn muốn. Nhưng, tôi biết rằng đôi khi khi bạn đăng mã, bạn sẽ xóa các chi tiết không quan trọng đối với điều bạn đang gặp sự cố. Vì vậy, có thể có một số VerticalFieldManager chứa số HorizontalFieldManager chứa số BasicEditField là cần thiết. Tôi sẽ để lại cho bạn ... nó sẽ chỉ là tùy chọn dọn dẹp, mặc dù.

3) Tôi đã thử nghiệm điều này trên trình mô phỏng 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); 
     } 
     } 
    } 

} 
+0

bạn có thể cho tôi biết Tôi có thể mã Trình nghe cho Người quản lý HorizontalFieldManager trong mã của tôi Nếu tôi tạo một trình lắng nghe FieldChangeListener mới; Và thêm nó như hfm.setListener (nghe); – Yatin

+0

@YAK, đã có trong đó (trong mã của tôi ở trên). Tôi đã thay đổi 'HorizontalFieldManager' ban đầu thành một lớp con riêng mới,' CustomHorizontalFieldManager'. Lớp đó triển khai thực hiện giao diện 'FieldChangeListener', có nghĩa là nó có phương thức' fieldChanged() '. Nếu bạn nhìn vào mã của tôi, phương thức 'fieldChanged()' là nơi tôi tự động tính toán độ dài của văn bản trong trường chỉnh sửa và gọi 'setVirtualExtent()'. Đó là toàn bộ mẹo tôi sử dụng để thực hiện công việc này. Chỉ cần sử dụng mã của tôi. – Nate

+0

và chỉ để làm rõ, nó không phải là đối tượng hfm mà bạn đặt người nghe trên. đó là trường chỉnh sửa cơ bản. do đó ** bên trong ** hàm tạo của trình quản lý trường ngang, tôi thiết lập trình quản lý trường làm trình lắng nghe cho trường chỉnh sửa. đó là cách nó biết khi văn bản đã thay đổi, và chiều rộng ảo cần phải được tăng (hoặc giảm). – Nate

Các vấn đề liên quan