从PopupScreen更新ButtonField的标签

时间:2012-08-07 09:56:25

标签: java blackberry

我在MainScreen上有一个ButtonField,我正在推送一个PopupScreen,我在其中添加了一个ObjectListfield。我想要做的是使用从PopupScreen的ObjectListfield中选择的元素更新MainScreen上ButtonField的标签。

请告诉我是否可以不使用Dialog类(我真的想使用PopupScreen而不是Dialog类)以及可以执行此操作的方法。如果会提供一些示例代码,我将不胜感激。

我添加了我的代码。

public final class MyScreen extends MainScreen {
    HorizontalFieldManager hfm;
    ButtonField btn;
    public MyScreen() {        
        // Set the displayed title of the screen       
        super();
        setTitle("MyTitle");


        btn = new ButtonField("label",ButtonField.CONSUME_CLICK);
        final mypopup mps = new mypopup();

        btn.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field,int context) {     
                UiApplication.getUiApplication().pushModalScreen(mps);      
            }
        });

        hfm = new HorizontalFieldManager();
        hfm.add(btn);
        add(hfm);
    }

    public void setlabel(String labelnew) {
        btn.setLabel(labelnew);
    }

    public String getlabel() {
        return this.btn.getLabel();
    }
}

class mypopup extends PopupScreen implements FieldChangeListener {

    String it;

    ObjectListField obj = new ObjectListField() {
        public boolean navigationClick(int status,int time) {
            int selectedindex=obj.getSelectedIndex();
            it=(String)obj.get(obj, selectedindex);

            UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());

            /*MyScreen my=new MyScreen();
            my.btn.setLabel(it);
            my.invalidate(); */
            //close();

            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    /* This im doing to see that setlabel and getlabel are
                                working properly */     
                    MyScreen my=new MyScreen(); 
                    my.setlabel(it);                 
                    String gt=my.getlabel();     
                    Dialog.alert(gt);            
                    my.hfm.invalidate();                             

                        //the label of button is changed but not updating in mainscreen.
                            }
            });

            return true;        
        }
    };

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

        String[] type=new String[] {"a","b","c","d"};
        obj.set(type);
        add(obj);       
     }

    public void fieldChanged(Field field, int context) {
        // TODO Auto-generated method stub
    }

} 

2 个答案:

答案 0 :(得分:0)

在一个管理器中添加按钮HorizontalFieldManager or VerticalFieldManager,在按钮上设置文本后,这样管理器就会失效

public final class MyScreen extends MainScreen
{

  ButtonField btn;
  public MyScreen()
 {        
// Set the displayed title of the screen       
super();
setTitle("MyTitle");


btn=new ButtonField("label",ButtonField.CONSUME_CLICK);
final mypopup mps=new mypopup();

btn.setChangeListener(new FieldChangeListener()
{
    public void fieldChanged(Field field,int context){

        UiApplication.getUiApplication().pushModalScreen(mps);

    }
});

HorizontalFieldManager hfmToholdButtons = new HorizontalFieldManager();
btn.setLabel(mps.gettext());
hfmToholdButtons.add(btn);
hfmToholdButtons.invalidate();
}
}

答案 1 :(得分:0)

您需要更改以下代码块,

MyScreen my = new MyScreen();
my.setlabel(it);
String gt = my.getlabel();
Dialog.alert(gt);
my.hfm.invalidate();

使用代码块

Screen scr = UiApplication.getUiApplication().getActiveScreen();
if (scr instanceof MyScreen) {
    MyScreen my = (MyScreen) scr;
    my.setlabel(it);
    my.invalidate();
}
相关问题