【问题标题】:Why button on screen not effect when I use postGlobalScreen to push this screen?当我使用 postGlobalScreen 推送此屏幕时,为什么屏幕上的按钮不起作用?
【发布时间】:2012-02-18 04:12:50
【问题描述】:

我编辑 Blackberry_App_Descriptor.xml 以使我的应用程序是“启动时自动运行”。我的应用程序有 3 个类,myApp、screen1 和 screen2。 MyApp 使用 main 方法来推送 screen1。在 screen1 中有一个按钮来推动屏幕 2。当我手动启动应用程序时它运行正常。 (点击App图标)

问题是:

我使用 RealTimeListener 始终检查每分钟的时间,如果是 1h30,它将推送 screen1,(我使用方法 postGlobalScreen 推送 Screen1)。它推动了成功。但是我可以使用这个屏幕1上的按钮,我点击它并没有推送到屏幕2。

我尝试使用备用入口点检查时间并推送 screen1,但结果相同。

谁能帮我解决和解释清楚这个问题?

// MyApp.java
   public class MyApp extends UiApplication implements RealtimeClockListener
    {
        /**
         * Entry point for application
         * @param args Command line arguments (not used)
         */ 

        public static void main(String[] args)
        {
            // Create a new instance of the application and make the currently
            // running thread the application's event dispatch thread.
            MyApp theApp = new MyApp();  
            theApp.enterEventDispatcher();
        }


        /**
         * Creates a new MyApp object
         */
        public MyApp()
        {        
            // Push a screen onto the UI stack for rendering.
            pushScreen(new Screen1());
            addRealtimeClockListener(this);


        }


        public void clockUpdated() {
            int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
            int minute = Calendar.getInstance().get(Calendar.MINUTE);
            if(hour==1 && minute == 30){    

                UiApplication.getUiApplication().pushGlobalScreen(new Screen1(),1,UiEngine.GLOBAL_MODAL);           
            }

        }



    }


//Screen1.java
public final class Screen1 extends MainScreen implements FieldChangeListener
{
    /**
     * Creates a new MyScreen object
     */
    ButtonField button;
    public Screen1()
    {        
        button = new ButtonField("Screen 1 ");
        button.setChangeListener(this);
        add(button);


    }
    public void fieldChanged(Field field, int context) {
        if(field==button){
            UiApplication.getUiApplication().pushScreen(new Screen2());

        }

    }

}

//Screen2.java
public final class Screen2 extends MainScreen implements FieldChangeListener
{
    /**
     * Creates a new MyScreen object
     */
    ButtonField button;
    public Screen2()
    {        
        button = new ButtonField("Screen2");
        button.setChangeListener(this);
        add(button);


    }
    public void fieldChanged(Field field, int context) {
        if(field==button){
            UiApplication.getUiApplication().pushScreen(new Screen1());
        }

    }
}

【问题讨论】:

    标签: blackberry


    【解决方案1】:

    请尝试以下。这里重点讲两点

    1)应用程序在后台运行

    2)将后台应用程序发送到前台

    package mypackage;
    
    import java.util.Calendar;
    
    import net.rim.device.api.system.RealtimeClockListener;
    import net.rim.device.api.ui.UiApplication;
    import net.rim.device.api.ui.UiEngine;
    
    public class MyApp extends UiApplication implements RealtimeClockListener
    {
        /**
         * Entry point for application
         * @param args Command line arguments (not used)
         */ 
        public  static MyApp theApp=null;
        public static void main(String[] args)
        {
            // Create a new instance of the application and make the currently
            // running thread the application's event dispatch thread.
            theApp = new MyApp();  
            theApp.enterEventDispatcher();
        }
    
    
        /**
         * Creates a new MyApp object
         */
        public MyApp()
        {        
            // Push a screen onto the UI stack for rendering.
            pushScreen(new Screen1());
            addRealtimeClockListener(this);
        }
    
    
        public void clockUpdated() {
            int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
            int minute = Calendar.getInstance().get(Calendar.MINUTE);
          //  if(hour==1 && minute == 30){    
            if(!theApp.isForeground())
            {
                UiApplication.getUiApplication().pushGlobalScreen(new Screen1(),1,UiEngine.GLOBAL_MODAL);           
            }
    
        }
    }
    

    screen1.java

    package mypackage;
    
    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.container.MainScreen;
    
    public final class Screen1 extends MainScreen implements FieldChangeListener
    {
        /**
         * Creates a new MyScreen object
         */
        ButtonField button;
        public Screen1()
        {        
            button = new ButtonField("Screen 1 ");
            button.setChangeListener(this);
            add(button);
    
    
        }
        public void fieldChanged(Field field, int context) {
            if(field==button){
                close();
                MyApp.theApp.requestForeground();
                UiApplication.getUiApplication().pushScreen(new Screen2());
    
            }
    
        }
        public boolean onClose() {
            MyApp.theApp.requestBackground();
            return true;
        }
    
    }
    

    screen2.java

    package mypackage;
    
    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.container.MainScreen;
    
    public final class Screen2 extends MainScreen implements FieldChangeListener
    {
        /**
         * Creates a new MyScreen object
         */
        ButtonField button;
        public Screen2()
        {        
            button = new ButtonField("Screen2");
            button.setChangeListener(this);
            add(button);
    
    
        }
        public void fieldChanged(Field field, int context) {
            if(field==button){
                UiApplication.getUiApplication().pushScreen(new Screen1());
            }
    
        }
    }
    

    【讨论】:

    • 太棒了!谢谢HelpMeToHelpYou,你是黑莓大师:)
    猜你喜欢
    • 2011-10-10
    • 1970-01-01
    • 2017-03-27
    • 2020-02-01
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    相关资源
    最近更新 更多