我对如何在 Blackberry JDE 中实现 FieldChangeListener 感到有点困惑。一种方法是让我的主类实现 FieldChangeListener,然后在其中添加一个 fieldchanged 方法,另一种方法是让我这样做:
FieldChangeListener listenerUS = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Something changed!");
pushScreen(_newScreen);
}
};
无论哪种方式,如果我尝试调用一个方法(例如 PushScreen 或我编写的自定义方法),我都会收到运行时错误。在 Debug模式下,我的打印语句也没有被显示。但是,如果我完全删除 fieldChanged 方法,它甚至无法编译,所以我非常确定它看到了代码?
我已将监听器添加到我希望它通过以下方式连接到的按钮:
but_temp.setChangeListener(this);
(在第一种情况下)或通过放置listenerUS.
一切似乎都已连接,但我的打印语句显示出来,如果我调用一个方法,我会收到运行时错误。
这有道理吗?我是否完全不知道如何在黑莓上使用监听器?
这里有我的整个代码的副本...
最佳答案
我看了你的代码,没有发现任何明显的错误。但是,我不会指定主应用程序类承担 FieldChangeListener 的职责。这不是它应该意识到的事情。我能为您做的最好的事情就是提供一个为 ButtonField 实现 FieldChangeListener 接口(interface)的示例应用程序。这不是一个解决方案,但也许随着您对代码的更好了解,您将能够挑选出与此示例不同的内容。希望对您有所帮助。
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.FieldChangeListener;
/**
* Test implementation of ButtonField.
*/
public class TestAppMain extends UiApplication
{
/**
* Default Constructor.
*/
private TestAppMain() {
pushScreen(new AppScreen());
}
/**
* App entry point.
* @param args Arguments.
*/
public static void main(String[] args) {
TestAppMain app = new TestAppMain();
app.enterEventDispatcher();
}
/**
* Main application screen.
*/
private static class AppScreen extends MainScreen
{
/**
* Default constructor.
*/
public AppScreen() {
LabelField title = new LabelField("Button Test Demo",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
// Create a button with a field change listener.
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ButtonField buttonField = (ButtonField) field;
System.out.println("Button pressed: " + buttonField.getLabel());
}
};
ButtonField buttonField = new ButtonField("Test Button", ButtonField.CONSUME_CLICK);
buttonField.setChangeListener(listener);
add(buttonField);
}
/**
* Handle app closing.
*/
public void close() {
Dialog.alert("Goodbye!");
System.exit(0);
super.close();
}
}
}
关于java - 黑莓 JDE FieldChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1433945/