2012-03-13 23 views
6

Tôi muốn xác thực số nhập vào hộp văn bản.Xác thực trong văn bản swt

Tôi muốn người dùng chỉ nhập số nguyên, thập phân trong hộp giữa giá trị tối đa và giá trị tối thiểu.

Làm cách nào để đảm bảo điều này?

Cảm ơn bạn.

Trả lời

3

Bạn có thể đăng ký ModifyListener bằng điều khiển văn bản và sử dụng nó để xác thực số.

txt.addModifyListener(new ModifyListener() { 

     @Override 
     public void modifyText(ModifyEvent event) { 
      String txt = ((Text) event.getSource()).getText(); 
      try { 
       int num = Integer.parseInt(txt); 
       // Checks on num 
      } catch (NumberFormatException e) { 
       // Show error 
      } 
     } 
    }); 

Bạn cũng có thể sử dụng addVerifyListener để ngăn các ký tự nhất định được nhập. Trong trường hợp được chuyển vào phương thức đó, có trường "doit". Nếu bạn đặt nó thành false, nó sẽ ngăn cản chỉnh sửa hiện tại.

11

Sử dụng một VerifyListener vì điều này sẽ xử lý dán, backspace, thay thế .....

Ví dụ: xác nhận

text.addVerifyListener(new VerifyListener() { 
    @Override 
    public void verifyText(VerifyEvent e) { 
    final String oldS = text.getText(); 
    final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end); 

    try { 
     BigDecimal bd = new BigDecimal(newS); 
     // value is decimal 
     // Test value range 
    } catch (final NumberFormatException numberFormatException) { 
     // value is not decimal 
     e.doit = false; 
    } 
    } 
}); 
+0

biểu thức từng phần như "4e-" nên được cho phép để có thể nhập "4e-3" – Stefan

3

Integer trong SWT

text = new Text(this, SWT.BORDER); 
text.setLayoutData(gridData); 
s=text.getText(); 
this.setLayout(new GridLayout()); 
text.addListener(SWT.Verify, new Listener() { 
    public void handleEvent(Event e) { 
     String string = e.text; 
     char[] chars = new char[string.length()]; 
     string.getChars(0, chars.length, chars, 0); 
     for (int i = 0; i < chars.length; i++) { 
     if (!('0' <= chars[i] && chars[i] <= '9')) { 
      e.doit = false; 
      return; 
     } 
     } 
    } 
}); 
0

thử đoạn mã sau:

/** 
* Verify listener for Text 
*/ 
private VerifyListener verfyTextListener = new VerifyListener() { 
    @Override 
    public void verifyText(VerifyEvent e) { 
     String string = e.text; 
     Matcher matcher = Pattern.compile("[0-9]*+$").matcher(string); 
     if (!matcher.matches()) { 
      e.doit = false; 
      return; 
     } 
    } 
}; 
0
  • Nếu bạn chỉ muốn cho phép Integer giá trị mà bạn có thể sử dụng một Spinner thay vì một văn bản cánh đồng.

  • Để xác thực giá trị kép, bạn phải xem xét các ký tự như E có thể được bao gồm trong Đôi và phần nhập một phần như "4e-" phải hợp lệ khi nhập. biểu một phần như vậy sẽ cung cấp cho một NumberFormatException cho Double.parseDouble (partialExpression)

  • Cũng xem câu trả lời của tôi lúc sau câu hỏi liên quan: How to set a Mask to a SWT Text to only allow Decimals

0

1.create một lớp utill thực hiện xác minh nghe

2.override verifytext method

3.thực hiện logic của bạn

4.create một đối tượng của lớp utill nơi bạn muốn sử dụng xác minh người nghe (trên văn bản)

5.text.addverifylistener (utillclassobject)

Ví dụ: - lớp 1. utill: -

public class UtillVerifyListener implements VerifyListener { 

@Override 
public void verifyText(VerifyEvent e) { 

    // Get the source widget 
    Text source = (Text) e.getSource(); 

    // Get the text 
    final String oldS = source.getText(); 
    final String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end); 

    try { 
     BigDecimal bd = new BigDecimal(newS); 
     // value is decimal 
     // Test value range 
    } catch (final NumberFormatException numberFormatException) { 
     // value is not decimal 
     e.doit = false; 
    } 
} 

2.sử dụng verifylistener trong lớp khác

public class TestVerifyListenerOne { 
public void createContents(Composite parent){ 

    UtillVerifyListener listener=new UtillVerifyListener(); 
    textOne = new Text(composite, SWT.BORDER); 
    textOne .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 
    textOne .addVerifyListener(listener) 

    textTwo = new Text(composite, SWT.BORDER); 
    textTwo .setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); 
    textTwo .addVerifyListener(listener); 

}}

0
text.addVerifyListener(new VerifyListener() { 
    @Override 
    public void verifyText(VerifyEvent e) { 
     Text text = (Text) e.getSource(); 

     final String oldS = text.getText(); 
     String newS = oldS.substring(0, e.start) + e.text + oldS.substring(e.end); 

     boolean isValid = true; 

     try { 

      if(! "".equals(newS)){ 
       Float.parseFloat(newS); 
      } 

     } catch (NumberFormatException ex) { 
      isValid = false; 
     } 

     e.doit = isValid; 
    } 
}); 
Các vấn đề liên quan