这个的用途先说一下,我以前用VB写程序,如果要设置一个标志通知其它窗口程序的设置已经更改了,只需要在module里改变一个变量的值就可以了,现在的黑莓jde不存在模块一说,全局变量也只存在于类文件内部,比如说我修改了软件设置,需要通知其它窗口根据新设置调整自己的布局,怎么办呢?下边是3个类文件,可以简单的实现通知功能,当然只是简单的通知,如果要传递更多的数据可以做相应的修改即可。
SampleApp.java
package code;
import net.rim.device.api.ui.UiApplication;
public class SampleApp extends UiApplication
{
public static void main( String[] args )
{
SampleApp theApp = new BacklightApp();
theApp.enterEventDispatcher();
}
public SampleApp()
{
pushScreen(new BacklightScreen());
}
}
SampleAppScreen.java
package code;
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.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
public class SampleAppScreen extends MainScreen
{
public SampleAppScreen()
{
super( MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR );
setTitle("My Test App");
ButtonField btnTest=new ButtonField("Test", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
btnTest.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
TestScreen ts=new TestScreen();
Dialog.alert("Before push screen, modified=" + ts.isModified());
//以模式窗口弹出,这点很重要!
UiApplication.getUiApplication().pushModalScreen(ts);
Dialog.alert("After pop screen, modified=" + ts.isModified());
ts=null;
}
});
add(btnTest);
}
}
TestScreen.java
package code;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
public class TestScreen extends MainScreen
{
private boolean modified=false;
public TestScreen()
{
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
ButtonField btnTest=new ButtonField("Click Me!", ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
btnTest.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
modified=!modified;
add(new LabelField("modified=" + modified));
}
});
add(btnTest);
}
public boolean isModified()
{
return modified;
}
}
这样在TestScreen关闭之后调用isModified()方法确认设置是否更改,其它窗口是否需要调整布局等等,灵感来自于DateField的调用方法