在黑莓中创建自定义输入对话框

时间:2012-03-26 12:25:05

标签: blackberry dialog

您好我是黑莓新手,我正在尝试在黑莓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 ==“”的值,并且我得到一个空白的对话框。  我只想要那个文本框值。请帮助我坚持这个......

2 个答案:

答案 0 :(得分:0)

navigationClick中使用TestDialog classprotected boolean navigationClick(int status, int time) { Dialog.alert(rd.getText()); return true; }它会起作用......

答案 1 :(得分:0)

@AMUL如果你没有得到特定的按钮点击事件,你也可以为此实现替代解决方案。实施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 !!!