本文介绍了mobileapplication.mobileevent BACK_BUTTON_PRESSED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的两个视图中有2个Android原生音频实例。当用户按下后退按钮并离开视图时,我正试图让音频停止,因为它没有自动发生。我查看了文档并看到了MobileEvent类。我试过没有运气实现它的构造函数。这是我的第一个应用程序,我只是为了这个目的而自己学习Java和JavaFX,所以一些帮助会很棒。我目前的尝试如下。

I've got 2 android native audio instances in two of my views. I'm trying to get the audio to stop when the user presses the back button and leaves the view as it's not happening automatically. I've looked at the documentation and seen the MobileEvent class. I've tried implementing it's constructor with no luck. This is my first app altogether and I've only just learnt Java and JavaFX on my own for this purpose so some help would be great. My current attempt is below.

    public void MobileEvent(javafx.event.EventTarget source,
               javafx.event.EventType<MobileApplication.MobileEvent> BACK_BUTTON_PRESSED) {
    service.backPressed();
}

这是胶子应用程序。

推荐答案

MobileEvent.BACK_BUTTON_PRESSED 这样的自定义事件的想法是你可以使用事件处理程序订阅它。

The idea of a custom event like MobileEvent.BACK_BUTTON_PRESSED is that you can subscribe to it using an event handler.

例如,如果您创建,你想在用户按下后退按钮时关闭它:

For instance, if you create a layer and you want to close it when the user presses the back button:

public BasicView(String name) {
    super(name);

    // create a custom layer
    MobileApplication.getInstance().addLayerFactory("My Layer", () -> new Layer() {
        private final Node root;
        private final double size = 300;

        {
            root = new StackPane(new Button("A custom layer"));
            root.setStyle("-fx-background-color: lightgreen;");
            getChildren().add(root);
            getApp().getGlassPane().getLayers().add(this);

            // Add event handler to listen to Android Back Button Pressed event, hiding the layer
            addEventHandler(MobileApplication.MobileEvent.BACK_BUTTON_PRESSED, e -> {
                    hide();
                    e.consume();
            });
        }

        @Override
        public void hide() {
            setShowing(false);
            super.hide(); 
        }

        @Override
        public void layoutChildren() {
            root.setVisible(isShowing());
            if (!isShowing()) {
                return;
            }
            root.resize(size, size);
            resizeRelocate(0, 0, size, size);
        }
    });

    Button button = new Button("Show Layer");
    button.setOnAction(e -> MobileApplication.getInstance().showLayer("My Layer"));

    VBox controls = new VBox(15.0, button);
    controls.setAlignment(Pos.CENTER);

    setCenter(controls);
}

如果您创建单一视图项目,请使用上面的代码段并部署它在Android设备上,您可以验证当您单击按钮时图层显示,如果您点击Android后退按钮,它将关闭图层。

If you create a Single View project, use the snippet above, and deploy it on an Android device, you can verify that when you click the button the layer shows up, and if you hit the Android back button, it will close the layer.

请注意,如果再次点击它,它将关闭应用程序:主视图已经有一个关于此事件的监听器,这就是应用程序关闭的原因。或者,如果您处于辅助视图中,则此事件将返回上一个视图。

Notice that if you hit it again, it will close the app: The home view already has a listener on this event, that's why the app gets closed. Or if you are in a secondary view, with this event you will return to the previous view.

虽然您可以在代码中的任何位置订阅此事件,例如我已经在上面的示例中完成了,您已经可以更轻松地跟踪其他事件了。例如, LifecycleEvent事件,如 SHOWING HIDING`,已被所有视图使用。

While you can subscribe to this event at any point in your code, like I've done in the example above, there are already other events that you can track more easily. For instance, the LifecycleEvent events, likeSHOWINGorHIDING`, are already used by all the Views.

因此,您可以向自定义视图添加任何这些事件的监听器:

So you can add to your custom view a listener to any of those events:

public BasicView(String name) {
    super(name);

    Label label = new Label("This is a custom view");

    VBox controls = new VBox(15.0, label);
    controls.setAlignment(Pos.CENTER);

    setCenter(controls);

    setOnShowing(e -> System.out.println("Showing Event"));

    setOnHiding(e -> System.out.println("Hiding Event"));
}

请注意,在您的情况下,您可以轻松找出用户何时离开查看,然后相应地做出反应调用服务来停止音频:

Note that in your case, you can easily find out when the user leaves the view, and then react accordingly calling the service to stop the audio:

    setOnHiding(e -> {
        Services.get(MyAudioService.class).ifPresent(service -> service.stop());
    });

这篇关于mobileapplication.mobileevent BACK_BUTTON_PRESSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:14