2012-03-26 86 views
0

嗨,我是黑莓新手,正在嘗試在黑莓7.0中的菜單單擊事件中創建一個自定義輸入對話框。一切工作正常,唯一的問題是,我無法檢索出現在對話框中的文本框的字符串。在黑莓中創建一個自定義輸入對話框

public class MyScreen extends MainScreen implements FieldChangeListener 
{ 
String host; 
private MenuItem mu; 
Dialog dg; 
TestDialog td; 

public MyScreen() 
{  
    final String choices[] = {"Ok","Cancel"}; 
    final int values[] = {Dialog.OK,Dialog.CANCEL}; 

    mu=new MenuItem("Configure IP", 1, 1){ 
     public void run() 
     { 
      UiApplication.getUiApplication().invokeLater(new Runnable() 
      { 
       public void run() 
       { 
        td=new TestDialog(choices, values); 
        dg=new TestDialog(choices, values); 
        int iResponse = dg.doModal(); 
        if(iResponse == 0) 
        { 
         String url=td.getUsernameFromField(); 
         if(url.equals("")) 
         { 
          Dialog.alert("Empty"); 
         } 
       else 
       { 
        Dialog.alert("Not Empty"); 
       } 
        } 
        else 
        { 
         System.out.println("Cancel Presses"); 
        } 
       } 
      });   
     } 
    }; 

    addMenuItem(mu); 
    setTitle("Custom Dialog box"); 
    } 
} 



final class TestDialog extends Dialog{ 

public RichTextField rd; 

public TestDialog(String choices[],int values[]){ 
    super("Enter Host address", choices,values,Dialog.OK,  
Bitmap.getPredefinedBitmap(Bitmap.INFORMATION), Dialog.GLOBAL_STATUS); 

    rd=new RichTextField(); 
    add(rd); 
    rd.setEditable(true); 
    XYEdges thickPadding = new XYEdges(10, 10, 10, 10); 
    Border roundedBorder = BorderFactory.createRoundedBorder(thickPadding, 
    Border.STYLE_SOLID); 
    rd.setBorder(roundedBorder); 
}  

    public String getUsernameFromField(){ 
    return rd.getText(); 
    } 
} 

所以運行代碼,點擊菜單按鈕後打開一個輸入對話框。現在,如果我在文本框中輸入任何內容,然後單擊確定,我會得到String url的值==「」,然後我得到一個對話框說空。 我只想要那個文本框的值。請幫我卡在這...

回答

0

@AMUL如果你沒有得到一個按鈕點擊事件比u還可以實現替代解決方案。執行PopupScreen,看起來像Dialog ..檢查下面的代碼,你可以輕鬆地獲得有關button字段的單擊事件。

import net.rim.device.api.ui.Field; 
import net.rim.device.api.ui.FieldChangeListener; 
import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.XYEdges; 
import net.rim.device.api.ui.component.ButtonField; 
import net.rim.device.api.ui.component.Dialog; 
import net.rim.device.api.ui.component.RichTextField; 
import net.rim.device.api.ui.container.PopupScreen; 
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 CustomDialog extends PopupScreen implements FieldChangeListener 
{ 
    public RichTextField rd; 
    ButtonField btnOK,btnCancel; 

public CustomDialog() 
{ 
    super(new VerticalFieldManager()); 

    rd=new RichTextField(); 
    rd.setEditable(true); 
    XYEdges thickPadding = new XYEdges(10, 10, 10, 10); 
    Border roundedBorder = BorderFactory.createRoundedBorder(thickPadding,Border.STYLE_SOLID); 
    rd.setBorder(roundedBorder); 

    btnOK = new ButtonField("OK"); 
    btnOK.setChangeListener(this); 

    btnCancel = new ButtonField("Cancel"); 
    btnCancel.setChangeListener(this); 


    add(rd); 
    add(btnOK); 
    add(btnCancel); 
} 

public void fieldChanged(Field field, int context) 
{ 
    // TODO Auto-generated method stub 
    if (field == btnOK) 
    { 
     UiApplication.getUiApplication().invokeLater(new Runnable() { 

      public void run() { 
       // TODO Auto-generated method stub 
       Dialog.alert(rd.getText()); 
      } 
     }); 

    } 
    else if(field == btnCancel) 
    { 
     close(); 
    } 
} 

public void close() { 
    close(); 
    super.close(); 
} 

} 


Push this screen on your Menu button click event or where ever u want. hopw it helps !!! 
+0

感謝兄弟,這是一個很大的幫助..... – AMUL 2012-03-26 16:15:12

0

使用navigationClick在你的TestDialog class那樣。 protected boolean navigationClick(int status, int time) { Dialog.alert(rd.getText()); return true; }它會工作...

+0

感謝您的早日答覆...我實施了這種方法......但我應該在哪裏調用它。我嘗試在OK事件上調用它,但它會拋出異常...請給出更多詳細信息... – AMUL 2012-03-26 14:13:55

+0

在您的TestDialog類中放置導航點擊方法。出構造..它會工作..我havnt得到微小的按鈕點擊事件。 。此方法適用於所有按鈕。 – Hitarth 2012-03-26 14:31:13

+0

我試過了,它的工作..沒有任何錯誤 – Hitarth 2012-03-26 14:31:34