【问题标题】:phonegap, android, backbutton: can't make it workphonegap,android,后退按钮:不能让它工作
【发布时间】:2012-07-16 18:56:25
【问题描述】:

Phonegap 1.8.1 -- 适用于 2.2 的 Android API -- jQuery 1.7.1 -- jQueryMobile 1.0

我制作了一个非常简单的应用程序来测试它,但我无法让它工作。在这里。

索引.HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>TestApp</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="jquery.mobile-1.1.0/jquery.mobile-1.1.0.min.css"/>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript" src="jquery.mobile-1.1.0/jquery.mobile-1.1.0.min.js"></script>
    <script type="text/javascript">
        function onDeviceReady() {

            document.addEventListener("backbutton", onBackButton);
        }

        function onBackButton(e){
            console.log("C'mon guv! Gimme a chance!");
        }

        document.addEventListener("deviceready", onDeviceReady, false);

    </script>
    <script type="test/javascript" src='cordova-1.8.1.js'></script>

</head>

<body>
<div id="homepage" data-role="page" data-theme="a">
    <div data-role="header">
        <h1>Work with me here</h1>
    </div>

    <div data-role="content">
        I am page 1.
        <a href="#char-1" data-role="button">Next</a>
    </div>
</div>

<div id="char-1" data-role="page" data-theme="a">
    <div data-role="content">HAHAHA!</div>
</div>


</body>
</html>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="2" android:targetSdkVersion="14"/>
    <supports-screens
        android:largeScreens="true"
        android:normalScreens="true"
        android:smallScreens="true"
        android:resizeable="true"
        android:anyDensity="true" />

    <application android:label="@string/app_name">
        <activity android:name="MyActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest> 

ADDENDUM - 我认为问题在于“deviceready”事件要么没有触发,要么在代码绑定到事件之前触发。不知道怎么解决。

如果我需要提供更多信息,请告诉我。我很难过。

【问题讨论】:

    标签: android jquery-mobile cordova


    【解决方案1】:

    据我所知,后退按钮按下事件在 onDeviceReady 内不起作用。您必须在每个页面显示中触发事件。就我而言,它是这样工作的,

    function onDeviceReady(){
        /*Back event handler for all pages navigation*/
        $(document).bind ('pageshow', function (e, data) {
            if ($.mobile.activePage.attr('id') == 'index') {
                document.addEventListener("backbutton", function () { 
                    setTimeout( function() {navigator.app.exitApp();}, 100 );
                }, true);
            }
            else{
                document.addEventListener("backbutton", function () {
                    setTimeout( function() {$.mobile.changePage("#index");}, 100 );
                }, true);
            }
        });
    
    
    }
    

    在我的应用程序中,当您从第一个屏幕按下返回按钮时,它会退出应用程序,如果您在任何页面内按下,它会自动进入第一页。

    【讨论】:

    • ?它在代码的下方。
    • 在head标签中初始化jquery js之前先放。
    • 怎么样? gist.github.com/3129987 仍然不适合我...我是否遗漏了一些不相关的东西、许可或奇怪的东西?
    【解决方案2】:

    我很惭愧地承认这一点。

    <script type="test/javascript" src='cordova-1.8.1.js'></script>
    

    这不是测试/javascript ...这是文本/javascript。

    :(

    【讨论】:

    • 也可能在时间轴上加载得太晚了。
    猜你喜欢
    • 1970-01-01
    • 2014-05-06
    • 2012-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    相关资源
    最近更新 更多